Android animation implementation method

  • 2020-06-19 11:42:46
  • OfStack

An example of Android animation is presented in this paper. Share to everybody for everybody reference. The specific analysis is as follows:

Animation is divided into three types:

Frame by frame animation, layout animation, and control animation

Control animation implementation

The custom animation effect is achieved by rewriting the applyTransformation (float interpolatedTime, Transformation t) function of Animation. In addition, the initialize (int width, int height, int parentWidth, int parentHeight) function is also implemented. This is a callback function that tells Animation the size of the target View. For example, setting the animation duration, setting Interpolator, setting the reference point for the animation, and so on.

OPhone repeatedly calls the applyTransformation function during the drawing of the animation. Each time the value of the parameter interpolatedTime changes, which changes from 0 to 1. When the parameter is 1, the animation is finished. The transformation matrix (matrix) is obtained through the parameter Transformation, and various complex effects can be achieved by changing the matrix.

Here is an example of a control animation:


Animation anim = new Animation() { 
  @Override 
  protected void applyTransformation(float interpolatedTime, Transformation t) { 
 if (interpolatedTime == 1) { 
   v.setVisibility(View.GONE); 
 } 
 else { 
   v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime); 
   v.requestLayout(); 
 } 
  } 
  @Override
  public boolean willChangeBounds() {
 return true;
  }
};

In the example, the height of 1 view gradually decreases from the original height to 0. When the animation ends, view disappears.

I hope this article has been helpful for your Android programming.


Related articles: