SoFunction
Updated on 2025-04-10

Detailed explanation of the usage of Android frame animation, tween animation, and attribute animation

In Android development, some animations are often used, so how do you use these animations in development?

Frame animation:Instead of making some shape changes for the View, it is used to play pictures one by one, such as some boot animations, similar to movie playback, using AnimationDrawable to play frame animations.
res/drawable 

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:andro 
 android:oneshot="true" 
 > 
 
 <item android:drawable="@drawable/g1" android:duration="200"></item> 
 <item android:drawable="@drawable/g2" android:duration="200"></item> 
 <item android:drawable="@drawable/g3" android:duration="200"></item> 
 <item android:drawable="@drawable/g4" android:duration="200"></item> 
 <item android:drawable="@drawable/g5" android:duration="200"></item> 
 
</animation-list> 
ImageView iv = (ImageView) findViewById(); 
(getResources().getDrawable(.frame_anim)); 
  
AnimationDrawable animationDrawable = (AnimationDrawable) (); 
  
//Set whether to execute only once//(false); 
  
(); 

Tween animation (View animation):If the View only does some animation and does not click or touch the View, you can use tween animation because the View animation will not change the position of the View, but will only do some rendering. The four transformation effects of View animation correspond to the four subclasses of Animation:TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation.

// With the center of the view as the scaling point, it will shrink from the initial state to the view without interruption and return to the view ScaleAnimation animation = new ScaleAnimation( 
  1.0f, 0.0f,//It becomes smaller little by little until it cannot be seen  1.0f, 0.0f, 
  Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f//Scaling in the middle ); 
 (BUBBLE_ENTER_CENTER_SCALE_TIME); 
 (); 
 (1); 
 (new () { 
  @Override 
  public void onAnimationStart(Animation animation) { 
  } 
 
  @Override 
  public void onAnimationEnd(Animation animation) { 
 
  homingBubbleView(true,position,view, current, endRatioFrame); 
  } 
 
  @Override 
  public void onAnimationRepeat(Animation animation) { 
  } 
 }); 
 (animation); 

Attribute animation:API11 new feature, if you not only do some animation operations on the View, but also click and touch on the View, you can use attribute animation because attribute animation will change the position of the View. The attribute animation classes include ValueAnimator, ObjectAnimator, and AnimatorSet.

Let's introduce two attribute animations below

ValueAnimator value animation, it is not used to make some animations of View, it is just used for an excessive animation between two values ​​(dividing the two differences equally according to time, and then adding up a little bit according to time). The system provides some excessive animations between two integer and floating point types. What should I do if the two values ​​are customized types? Android provides a() method, one of which is the TypeEvaluator type (type estimator). TypeEvaluator is an interface, which gives developers an extension. There is a public Object evaluation(float fraction, Object startValue, Object endValue) method in the interface. This method will be called continuously during the ValueAnimator animation. The fraction is the change rate between 0-1. StartValue is the (custom type) start value, endValue is the (custom type) end value, and the return type is the custom type. You can calculate how the value during the period should change according to your own needs (such as it can be the running track)

public class FloatEvaluator implements TypeEvaluator { 
 public Object evaluate(float fraction, Object startValue, Object endValue) { 
 float startFloat = ((Number) startValue).floatValue(); 
 return startFloat + fraction * (((Number) endValue).floatValue() - startFloat); 
 } 

use

ValueAnimator mAnimatorEnetr = (new FloatEvaluator(getContext()), 0,10); 
  (new () { 
  @Override 
  public void onAnimationUpdate(ValueAnimator animation) { 
   (Float) ();//This is the return of the ever-changing value   
  } 
  }); 
  (1000); 
  (new () { 
  @Override 
  public void onAnimationStart(Animator animation) { 
 
  } 
 
  @Override 
  public void onAnimationEnd(Animator animation) { 
   
  } 
 
  @Override 
  public void onAnimationCancel(Animator animation) { 
 
  } 
 
  @Override 
  public void onAnimationRepeat(Animator animation) { 
 
  } 
  }); 
  (); 

ObjectAnimator animation, it makes some attributes worth changing for view. It not only changes the value excessively, but also sets the changed value to the attribute to change, so that it produces an animation effect.
(view,"translationX",10,20).setDuration(100).start(); Parameter view is the view to generate animation, the "translationX" attribute, and the parameters followed are the interval of change
When ObjectAnimator does attribute animation, it will not change the left, top, right, bottom values ​​of the view. It only changes the values ​​of translationX and translationY. The relationship between these parameter values ​​is x = left + translationX  , y = top + translationY. During translation, only the values ​​of x and translationX , y and translationY will be changed. where x and y are the coordinates of the upper left corner of the View.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.