Android system provides three kinds of animation: frame animation, gap animation and attribute animation. Here we analyze and summarize the attribute animation.
Attribute animation is more powerful than frame animation and gap animation. Frame animation and gap animation can only be applied to View and its subclasses. Attribute animation can modify the attribute value of any object, and the attribute value can be changed automatically within a specified period of time to realize more complex animation according to the change of object attribute value.
1. Common settings of attribute animation
//Set attribute animation duration animator.setDuration(2000); //Set attribute interpolator animator.setInterpolator(new AccelerateInterpolator()); //Sets the number of repetitions of the attribute animation animator.setRepeatCount(0); //Sets the time to delay the playback of the attribute animation animator.setStartDelay(0); //Set the attribute animation estimator to control the final attribute value (API22) animator.setCurrentFraction(0.5f); //Set the current playback time, and its value is within the Duration range animator.setCurrentPlayTime(1000); //Animate the attribute estimator to control the final attribute value animator.setEvaluator(new IntEvaluator()); //Set attribute animation monitor animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { Log.i(TAG, animation.getAnimatedValue() + ""); // } });
2.ValueAnimator
ValueAnimator provides a simple timing engine, which is used to calculate the animation value according to the set duration and other attributes when executing the animation, and then set the animation value to the appropriate target object. The interpolator used is acceleratedeelerateinterpolator by default, which means that the animation is slow at the beginning and end, and the animation is accelerated in the middle.
In ValueAnimator, some estimators IntEvaluator and FloatEvaluator have been processed internally, that is, if the ofInt and ofFloat methods are used as the attribute values of animation, ValueAnimator will automatically process the changes of int and float values. ValueAnimator can be created using code or xml. Example:
ValueAnimator animator = ObjectAnimator.ofFloat(0, 100); animator.setDuration(2000); animator.start(); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float progress = (float) animation.getAnimatedValue(); mProgressBar.setProgress((int) progress); } });
3.ObjectAnimator is a subclass of ValueAnimator, which can support the setting of animation attributes on the target object. In its construction method, specify the name of the target object and the corresponding animation attribute through parameters, and then execute the setter method of the corresponding animation attribute accordingly to finally complete the execution of animation, In other words, the ObjectAnimator finally calls the setter method of the target object to change the attribute value of the target object, and then changes the attribute of the target object accordingly, so as to achieve the animation effect of the target object. Take transparency as an example:
private void alpha(){ ObjectAnimator animator = ObjectAnimator.ofFloat(ivImage,"alpha",1f,0,2f); animator.setDuration(3000); //Other attribute animation settings //... animator.start(); }
The animation of translation, rotation and scaling is implemented in the same way as above:
Alpha controls the transparency of the View
TranslationX controls the displacement in the X direction
TranslationY controls the displacement in the Y direction
Scalexcontrols the scaling factor in the X direction
ScaleY controls the scaling factor in the Y direction
Rotation controls the number of degrees of rotation with the screen direction as the axis
RotationX controls the number of degrees of rotation on the X axis
RotationY controls the number of degrees of rotation with the Y axis as the axis
4. Interpolator and estimator
The time interpolator represents the change law of the animation during the whole animation, such as acceleration, deceleration and so on.
Android has many built-in interpolators, which basically cover most situations in actual development. If the built-in interpolator does not meet the requirements, you can also customize the interpolator.
The type evaluator represents the specific changes of attribute values at each time during the whole animation.