Android animation details 3 animation API Overview
· Differences between property animation and view animation
The view animation system provides the ability to only animation View objects, so if you want to animation non-View objects, You need to implement code by yourself. In fact, the view animation system is forced to only animation a few attributes of the View, such as scaling and rotation, rather than the background color.
Another disadvantage of view animation is that it only modifies the drawing position of the View, rather than the actual position of the View. For example, if you animation a movement across the screen, the button is correctly drawn, but you can click it but the position remains unchanged, therefore, you must implement your own logic to process it.
When an attribute animation system is used, this restriction is completely removed, and you can animation any attributes of any object (View or non-View) and the object itself will actually be modified. In addition, the property animation system has more powerful support for animations. In the upper layer, you assign an animation to the animation attributes, such as color, position, or size, and then define the animation parameters, such as interpolation algorithms and synchronization of multiple animations.
However, the view animation system only requires a small amount of time to set and does not require too much code. If the view animation meets your needs, or can work as expected in your old code, you do not need to use the attribute animation system. These two animation systems may also be used in some cases.
· API Overview
You can find the APIs of most property animation systems in android. animation. Because the view animation system has defined many interpolation algorithms in android. view. animation, you can also use them in the property animation system. The following table describes the main components of the property animation system.
The Animator class provides the basis for creating an animation. Generally, you should not directly use this class because it only provides the least functionality. The following subclasses are derived from Animator.
Table 1. Animators
Class |
Description |
ValueAnimator |
The main base class of property animation. It calculates the value of the property to be animated. It has all the core functions used to calculate the animation value. It contains parameters such as the time information of each animation, whether to play the video repeatedly, and the listener that receives the update event. You can also set how to calculate the custom type. An animation has two attributes: Calculate the animation value and set these values to an object or attribute. ValueAnimator is not responsible for the second entry. Therefore, you must listen for ValueAnimator value updates and modify the object to be animated according to your logic. |
ObjectAnimator |
A subclass of ValueAnimator. Allows you to set the attributes of the target object and object to be animated. This class updates attributes when calculating new animation values. Most of the time you should choose to use ObjectAnimator, because it is easier to process object animations. However, you may want to use ValueAnimator directly in some cases, because ObjectAnimator has many restrictions, such as the need for an object to have a specific operator method. |
AnimatorSet |
Provides a mechanism to control the animation relationships in an animation group. You can set multiple animations to play together, play separately, or play in a delayed manner. |
Evaluator tells the property animation system how to calculate the value of an attribute. Based on the time information provided by the animation class, the animation start value and end value are used to calculate the animation value. The property animation system provides the following evaluator:
Table 2. Evaluator
Class/Interface |
Description |
IntEvaluator |
Calculate the default evaluator of the int type attribute. |
FloatEvaluator |
Calculates the default evaluator of a float attribute. |
ArgbEvaluator |
Calculates the default evaluator of the color attribute represented by a hexadecimal value. |
TypeEvaluator |
An interface that allows you to create your own evaluator. If the property of the object to be animated is not int, float, or color, you must implement the TypeEvaluator interface to define how to calculate the animation value of the object property. If you want to change the default behavior of processing int, float, and color attributes, you can also specify custom TypeEvaluator for them. |
The time interpolation tool defines how time functions calculate animation values. For example, you can specify that the animation process is linear, which means that the animation is moving evenly, or specify a non-linear time, such as acceleration at the beginning and deceleration at the end. Table 3 describes the interpolation devices in android. view. animation. If the existing interpolation tool cannot meet your requirements, you can implement the TimeInterpolator interface to create your own interpolation tool.
Table3. interpolation tool
Class/interface |
Description |
AccelerateDecelerateInterpolator |
A slow interpolation tool in the middle. |
AccelerateInterpolator |
A constantly accelerated interpolation tool. |
AnticipateInterpolator |
The interpolation tool that first moves backward and then vigorously moves forward. |
AnticipateOvershootInterpolator |
First backward, then forward, exceed the end value and then return to the end value interpolation. |
BounceInterpolator |
The last hop is the interpolation. |
CycleInterpolator |
The interpolation tool for loop playback. |
DecelerateInterpolator |
The interpolation tool that keeps slowing down. |
LinearInterpolator |
The interpolation tool for fixed speed playback. |
OvershootInterpolator |
First rush forward until the end value is exceeded and then return to the interpolation tool of the start value. |
TimeInterpolator |
This allows you to implement your own interpolation interface. |