Common Codes for android Animation

Posted by Toxinhead on Tue, 14 May 2019 21:14:30 +0200

  • Combined animation

    Implementing composite animation mainly depends on the class AnimatorSet, which provides a play() method. If we pass an Animator object (Value Animator or Object Animator) into this method, we will return an instance of AnimatorSet.Builder. AnimatorSet.Builder includes the following four methods:
    • after(Animator anim) inserts an existing animation into the incoming animation and executes it
    • after(long delay) Delays existing animations after specified milliseconds
    • before(Animator anim) Execute an existing animation before it is inserted into the incoming animation
    • with(Animator anim) Execute both existing animation and incoming animation

    With these four methods, we can complete the logic of combination animation. For example, if we want TextView to move out of the screen into the screen first, and then start to rotate 360 degrees, while doing fade-in and fade-out operations, we can write as follows:

    1. ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);  
    2. ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);  
    3. ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);  
    4. AnimatorSet animSet = new AnimatorSet();  
    5. animSet.play(rotate).with(fadeInOut).after(moveIn);  
    6. animSet.setDuration(5000);  
    7. animSet.start();  
  • Animator listener


    In many cases, we want to be able to monitor the various events of the animation, such as when the animation starts and ends, and then perform some logical processing at the beginning or end of the animation. This function is fully achievable. The Animator class provides an addListener() method, which receives an Animator Listener. We just need to implement this Animator Listener to listen to all kinds of animation events.

    Object Animator is inherited from Value Animator, and Value Animator is inherited from Animator, so whether Value Animator or Object Animator can use addListener() method. In addition, AnimatorSet is also inherited from Animator, so addListener() is a general method.

    The code to add a listener is as follows:

    1. anim.addListener(new AnimatorListener() {  
    2.     @Override  
    3.     public void onAnimationStart(Animator animation) {  
    4.     }  
    5.   
    6.     @Override  
    7.     public void onAnimationRepeat(Animator animation) {  
    8.     }  
    9.   
    10.     @Override  
    11.     public void onAnimationEnd(Animator animation) {  
    12.     }  
    13.   
    14.     @Override  
    15.     public void onAnimationCancel(Animator animation) {  
    16.     }  
    17. });  

    As you can see, we need to implement four methods in the interface. The onAnimationStart() method is called at the beginning of the animation, the onAnimationRepeat() method is called when the animation is repeated, the onAnimationEnd() method is called at the end of the animation, and the onAnimationCancel() method is called when the animation is cancelled.

    But maybe a lot of times we don't want to listen to so many events, maybe I just want to listen to the end of the animation event, so it's very tedious to implement all four interfaces once every time. Never mind, Android provides an adapter class called AnimatorListener Adapter, which can solve the tedious problem of implementing interfaces, as follows:

    1. anim.addListener(new AnimatorListenerAdapter() {  
    2. });  
    Here we pass in the adapter object to the addListener() method. Since each interface has been implemented in the AnimatorListener Adapter, there is no need to implement any method here and no error will be reported. So if I want to listen to the animation to end this event, I just need to rewrite this method separately, as follows:
    1. anim.addListener(new AnimatorListenerAdapter() {  
    2.     @Override  
    3.     public void onAnimationEnd(Animator animation) {  
    4.     }  
    5. });  
  • Writing Animation with XML

  • <animator> Value Animator in the corresponding code
  • Object Animator > Object Animator in the corresponding code
  • <set> AnimatorSet in the corresponding code

So let's say we want to achieve a smooth transition from 0 to 100, which can be written in XML:

  1. <animator xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:valueFrom="0"  
  3.     android:valueTo="100"  
  4.     android:valueType="intType"/>  
If we want to change the alpha attribute of a view from 1 to 10, we can write as follows:
  1. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:valueFrom="1"  
  3.     android:valueTo="0"  
  4.     android:valueType="floatType"  
  5.     android:propertyName="alpha"/>  
In fact, XML animation writing is still very readable, the above content I believe you can understand without my explanation.


In addition, we can also use XML to complete complex combination animation operations, such as moving a view out of the screen into the screen, then starting to rotate 360 degrees, while rotating fade-in and fade-out operations, you can write as follows:

  1. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:ordering="sequentially" >  
  3.   
  4.     <objectAnimator  
  5.         android:duration="2000"  
  6.         android:propertyName="translationX"  
  7.         android:valueFrom="-500"  
  8.         android:valueTo="0"  
  9.         android:valueType="floatType" >  
  10.     </objectAnimator>  
  11.   
  12.     <set android:ordering="together" >  
  13.         <objectAnimator  
  14.             android:duration="3000"  
  15.             android:propertyName="rotation"  
  16.             android:valueFrom="0"  
  17.             android:valueTo="360"  
  18.             android:valueType="floatType" >  
  19.         </objectAnimator>  
  20.   
  21.         <set android:ordering="sequentially" >  
  22.             <objectAnimator  
  23.                 android:duration="1500"  
  24.                 android:propertyName="alpha"  
  25.                 android:valueFrom="1"  
  26.                 android:valueTo="0"  
  27.                 android:valueType="floatType" >  
  28.             </objectAnimator>  
  29.             <objectAnimator  
  30.                 android:duration="1500"  
  31.                 android:propertyName="alpha"  
  32.                 android:valueFrom="0"  
  33.                 android:valueTo="1"  
  34.                 android:valueType="floatType" >  
  35.             </objectAnimator>  
  36.         </set>  
  37.     </set>  
  38.   
  39. </set>  

Finally, the XML file is written, so how do we load the file into the code and start the animation? Just call the following code:

  1. Animator animator = AnimatorInflater.loadAnimator(context, R.animator.anim_file);  
  2. animator.setTarget(view);  
  3. animator.start();  

Topics: Android xml Attribute