一个简单的动画封装库 - morphos

2017-06-06      118      Android
项目简介

Morphos

They say a pic is worth a 1000 words. Is it true to admit a .gif is worth a 1000 pics?

Include in your project

In your root/build.gradle

allprojects {
  repositories {
  ...
  maven { url 'https://jitpack.io' }
  }
}

In your app/build.gradle

dependencies {
  compile 'com.github.rjsvieira:morphos:1.0.0'
}

Introducing Morphos : an animation wrapper. Morphos will take care of your views' animations without you having to write all that boring boilerplate code.

Initialization

Morphos are easy to interact with. Go ahead and create the following simple Morpho :

View viewToAnimate = ... ;
Morpho morph = new Morpho(viewToAnimate)
  .translate(50,50,0,1500) // will translate the view (x=50,y=50,z=0) in 1500 milliseconds, 
  .rotationXY(45,45,2000); // will rotate the view by 45 degrees on both the X-axis and Y-axis in 2000 milliseconds

You can then animate it by doing

morph.animate();

Which will then animate the desired Morphos using the default animation type (SEQUENTIAL).

What if I want to reverse the animation? Sure, just do :

morph.reverse();

Configuration

Create a Morpho

Morpho morphoOne = new Morphos(view);

Configure the Morphos' animations

As of the first release, Morphos supports the 7 most basic and common animations. Since Morphos has plenty of combinations for interpolation, animation type, duration, etc, the user is allowed to configure them according to his needs. Every animation configuration method returns the Morphos object itself, thus allowing the user to chain his preferred animations.

Note : If the user does not specify the duration and/or interpolator, the animation will assume a 0 second duration and the default interpolator.

alpha(double value)
alpha(double value, int duration)
alpha(double value, int duration, Interpolator interpolator)

scale(double valueX, double valueY)
scale(double valueX, double valueY, int duration)
scale(double valueX, double valueY, int duration, Interpolator interpolator)

translationX(AnimationTarget target, float valueX)
translationX(AnimationTarget target, float valueX, int duration)
translationX(AnimationTarget target, float valueX, int duration, Interpolator interpolator)

translationY(AnimationTarget target, float valueX)
translationY(AnimationTarget target, float valueX, int duration)
translationY(AnimationTarget target, float valueX, int duration, Interpolator interpolator)

translationZ(AnimationTarget target, float valueX)
translationZ(AnimationTarget target, float valueX, int duration)
translationZ(AnimationTarget target, float valueX, int duration, Interpolator interpolator)

translation(AnimationTarget target, float valueX, float valueY, float valueZ)
translation(AnimationTarget target, float valueX, float valueY, float valueZ, int duration)
translation(AnimationTarget target, float valueX, float valueY, float valueZ, int duration, Interpolator interpolator)

dimensions(float width, float height)
dimensions(float width, float height, int duration)
dimensions(float width, float height, int duration, Interpolator interpolator)

rotationXY(AnimationTarget target, double degreesX, double degreesY)
rotationXY(AnimationTarget target, double degreesX, double degreesY, int duration)
rotationXY(AnimationTarget target, double degreesX, double degreesY, int duration, Interpolator interpolator)

rotation(AnimationTarget target, double degrees)
rotation(AnimationTarget target, double degrees, int duration)
rotation(AnimationTarget target, double degrees, int duration, Interpolator interpolator)

Set a Listener to track the Morphos' animation process

morphoOne.setListener(new Animator.AnimatorListener() {
  @Override
  public void onAnimationStart(Animator animator) {
      System.out.println("Start");
  }

  @Override
  public void onAnimationEnd(Animator animator) {
      System.out.println("End");
  }

  @Override
  public void onAnimationCancel(Animator animator) {

  }

  @Override
  public void onAnimationRepeat(Animator animator) {

  }
})

Animate() the Morpho

This can be done through one of the several methods created just to make the invocation easier. The animate() method's default values are :

AnimationType : SEQUENTIAL - All animations are executed one sequentially Duration : -1 - Since no duration was specified, -1 is assumed thus executing every animation in its given duration. For example : morphoOne.translate(100,0,0,3000).scale(2,2,2000).animate() will execute the translation animation in 3 seconds, followed by an upscale animation of 2 seconds Interpolator - The overall interpolator (LinearInterpolator)

animate()
animate(AnimationType type, int duration)
animate(AnimationType type, int duration, Interpolator interpolator)

Reverse

After executing animate(), the user can rollback the animation by invoking the reverse() method. The reverse method works just like the animate() method, having the same combinations and parameters.

reverse()
reverse(AnimationType type, int duration)
reverse(AnimationType type, int duration, Interpolator interpolator)

Cancel

If the user wishes to cancel the on-going animation by any reason, he can do so by invoking the cancel method :

morphoOne.cancel();

updateView

Like the method explicitly indicates, the user can update the view associated with the Morpho. This will clear every animation already configured for the given object. updateView(View v) also invokes reset();

morphoOne.updateView(newView);

Reset

If by any chance the user wants to reset he Morpho and re-build it from scratch, he can do so by invoking the reset() method

morphoOne.reset()

Dispose

Last but not least, if they user wishes to discard the Morphos object, he can invoke the dispose() method, thus clearing and preparing the Morphos' inner variables for garbage collection.

morphoOne.dispose();