We are familiar with Frame animation and Tween animation before Android 3.0, so we won’t explain it here. Today I’m going to talk about Property animation introduced in 3.0.
ValueAnimator; ObjectAnimator; ViewPropertyAnimator
Then use ValueAnimator to focus on two concepts: TypeEvaluator; TimeInterpolator
Can say, make clear above 5 points, all animation is under control
ValueAnimator
ValueAnimator is the core of the entire property animation mechanism. The animation mechanism is implemented by continuously manipulating values. ValueAnimator is responsible for the animation transition between the initial value and the end value.
ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); anim.setDuration(300); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { float currentValue = (float) animation.getAnimatedValue(); Log.d("TAG", "cuurent value is " + currentValue); }}); anim.start();Copy the code
ObjectAnimator
ObjectAnimator is probably the most commonly encountered class compared to ValueAnimator, because ValueAnimator does nothing more than a smooth animation transition to a value, but we don’t really use this feature in many situations. ObjectAnimator, on the other hand, can animate any property of any object directly, such as the View’s alpha, rotation, translationX, and scaleY. They can fade in and out, rotate, move horizontally and zoom vertically.
ObjectAnimator animator = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
animator.setDuration(5000);
animator.start();Copy the code
AnimatorSet
AnimatorSet provides a mechanism for grouping multiple animations into a group and setting the sequence of the animations in the group, such as simultaneous playback, sequential playback, etc.
- After (Animator anim) inserts the existing animation after the incoming animation
- After (long delay) Executes the existing animation after the specified millisecond delay
- Before (Animator anim) inserts an existing animation before the incoming animation
- With (Animator anim) executes an existing animation and an incoming animation simultaneously
ObjectAnimator moveIn = ObjectAnimator.ofFloat(textview, "translationX", -500f, 0f);
ObjectAnimator rotate = ObjectAnimator.ofFloat(textview, "rotation", 0f, 360f);
ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(textview, "alpha", 1f, 0f, 1f);
AnimatorSet animSet = new AnimatorSet();
animSet.play(rotate).with(fadeInOut).after(moveIn);
animSet.setDuration(5000);
animSet.start();Copy the code
TypeEvalutors
The evaluate() method uses three parameters. The first parameter, fraction, is very important. This parameter is used to indicate the completion of the animation. Subtract the initial value from the end value, find the difference between them, multiply the fraction, and add the initial value to get the value of the current animation.
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);}
}Copy the code
TimeInterpolator
Time interplator defines the way in which attribute values change, such as linear uniform change, slow at first and gradually fast. In the Property Animation it’s TimeInterplator, in the View Animation it’s Interplator, they’re the same, before 3.0 there was only Interplator, After 3.0, the implementation code moved to TimeInterplator. Interplator inherits from TimeInterplator without any additional code inside.
- AccelerateInterpolator accelerates at first by slow interpolator
- Activity Decelerator, start fast and then slow down
- AccelerateDecelerateInterolator acceleration deceleration, after first started slowly, at the end of the middle speed
- AnticipateInterpolator reverse, in which a paragraph is changed in the opposite direction before being accelerated
- AnticipateOvershootInterpolator reverse the springback, first change to the opposite direction, to speed up play
- BounceInterpolator jumps, the value will jump when approaching the target value
- CycleIinterpolator loop, an animation that loops a certain number of times and changes its value to a sine function
- LinearInterpolator linear, linear uniform change
- OvershottInterpolator bounts back, eventually exceeding the desired value and then slowly changing to the desired value
private void startAnimation() { Point startPoint = new Point(getWidth() / 2, RADIUS); Point endPoint = new Point(getWidth() / 2, getHeight() - RADIUS); ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), startPoint, endPoint); anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { currentPoint = (Point) animation.getAnimatedValue(); invalidate(); }}); anim.setInterpolator(new BounceInterpolator()); anim.setDuration(3000); anim.start(); }Copy the code
ViewPropertyAnimator
ViewPropertyAnimator is not an advanced technique, it is very simple to use, but unlike all the property animations we have learned, it was not introduced in 3.0, but was added in 3.1.
textview.animate().x(500).y(500).setDuration(5000);Copy the code
The source code is uploaded to GitHub