项目作者: russelarms

项目描述 :
Animations driven by finger movement
高级语言: Java
项目地址: git://github.com/russelarms/OffsetAnimator.git
创建时间: 2017-05-09T21:13:27Z
项目社区:https://github.com/russelarms/OffsetAnimator

开源协议:MIT License

下载


OffsetAnimator

OffsetAnimator lets animate objects basing on touchevents, so users can be engaged in an animation process.

Yellow submarine

Usage

Add the library to your project:

1) Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

2) Add the dependency:

  1. dependencies {
  2. compile 'com.github.russelarms:offsetanimator:1.0.2'
  3. }

Requirements

Min sdk version is 11.
The library doesn’t have any transitive dependencies.

Tutorial

@russel.arms/android-animation-driven-by-finger-movement-offsetanimator-2833fce6847c">A Comprehensive tutorial.

Scene

To create viewpager-based animation you should bind a scene to position updates:

  1. ViewPagerAnimatorAdapter animatorAdapter = new ViewPagerAnimatorAdapter(scene.getScene());
  2. viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  3. @Override
  4. public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  5. super.onPageScrolled(position, positionOffset, positionOffsetPixels);
  6. animatorAdapter.onPageScrolled(position, positionOffset);
  7. }
  8. });

Then create a scene instance and pass it a root view and an initializer function:

  1. Scene.create(getRootView(), () -> initSteps());

Scripting

Script desired animation like this:

  1. private void initSteps() {
  2. scene.page(0).step(0)
  3. .createAnimation(ocean.getY(), ocean.getY() - convertDIPToPixels(getContext(), 120))
  4. .setStartThreshold(0.5f)
  5. .setDuration(0.8f)
  6. .setInterpolator(new DecelerateInterpolator())
  7. .setListener(value -> ocean.setY(value));
  8. }

A single page is taken as 1.0f. So duration 0.5f is a half of a distance.

Lazy initialization

It is not always convenient to specify exact view positions after some script done, so we can pass a lazy initializer. In this example animator will be initialized only on page 3 with corresponding coordinates:

  1. scene.page(3).step(3)
  2. .createAnimation(() -> AnimatorFactory.createAnimator(fishLeftBottom.getY(), fishLeftBottom.getY() + screenDimensions.y / 2))
  3. .setDuration(0.5f)
  4. .setListener(value -> fishLeftBottom.setY(value));

Here’s how to set the rotation:

  1. scene.page(2).step(2)
  2. .createAnimation(0, 90)
  3. .setDuration(0.25f)
  4. .setListener(value -> submarine.setRotation(value));

Interpolators

We can specify an interpolator (from android.view.animation). The library ships its own SpringInterpolator:

  1. scene.page(1).step(0)
  2. .createAnimation(1926, 1032)
  3. .setInterpolator(new SpringInterpolator(0.8f))
  4. .setListener(value -> submarine.setY(value));

Arc animations

Create arc animation:

  1. scene.page(2).step(1)
  2. .createAnimation(() -> AnimatorFactory.createArcAnimator(submarine,
  3. Utils.centerX(submarine),
  4. convertDIPToPixels(getContext(), 48),
  5. submarine.getX() + submarine.getWidth() / 2,
  6. Utils.centerY(submarine),
  7. 180f, Side.RIGHT))
  8. .setStartThreshold(0.5f)
  9. .setDuration(0.5f);

While standard OffsetAnimator requires user to set listeners and update fields by hand, arc animator doesn’t need listener: it assigns value implicitly.

Inheritance

OffsetAnimator is open to be inherited:

  1. public class AnotherOffsetAnimator extends OffsetAnimator {
  2. public AnotherOffsetAnimator(float x1, float x2) {
  3. super(x1, x2);
  4. }
  5. @Override
  6. public void animate(float position) {
  7. super.animate(position);
  8. }
  9. }

and to be used with a scene:

  1. scene.page(3).step(1)
  2. .createAnimation(() -> new AnotherOffsetAnimator(fishRight.getX(), fishRight.getX() + convertDIPToPixels(getContext(), 160)))
  3. .setDuration(0.5f)
  4. .setListener(value -> fishRight.setX(value));

License

OffsetAnimator is available under the MIT license. See the LICENSE file for more info.
The library uses some code from https://github.com/asyl/ArcAnimator for arc animations.

The submarine and the fish images picked from freepik.com:
Designed by Freepik
Background vector created by Freepik

Copyright 2017 russelarms.