The last one brings you the animation of mending room in view animation. Today, I'd like to introduce the ValueAnimator in PropertyAnimation
- Difference between view animation and attribute animation
- Attribute animation ValueAnimator.onInt use
- Attribute animation ValueAnimator.onFloat use
- Summary of onInt and onFloat:
- Listener method
- Cancel listening
- Clone and delay operation
- Git address link: [langyangyang]( https://github.com/langyangyangzzZ/Animation_ ().
- Reference code: [start]( https://blog.csdn.net/harvic880925/article/details/50525521/ .
Difference between view animation and attribute animation
- Different introduction time: View Animation was introduced at API Level 1. Property Animation was introduced by API Level 11, that is, Android 3.0 started to have Property Animation related APIs.
- Different package name: View Animation in package android.view.animation Medium. And the Property Animation API is in the package android.animation Medium.
- The naming of animation classes is different: in View Animation, the names of animation classes are all XXXXAnimation, while in Property Animator, the names of animation classes are XXXXAnimator
- View animation only changes the state of the control and does not change the properties of the control. Two property animation directly changes the properties of the control
Examples of patching animation are Kangkang effect:
As can be seen from the figure:
- Click to move to a location that doesn't work at all, which also proves that it just changes the 'body' of the animation, and the 'soul' of the control is still in the original place
Attribute animation effect;
You can see intuitively that where the control finally moves to, where it will be, and the click event effect will not appear in the original position
Attribute animation ValueAnimator.onInt use
Parametric construction
ValueAnimator.ofInt(int... values)
//Set where to move ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 400,200,600); valueAnimator.setDuration(2000); //Monitor the change of ofInt through addUpdateListener valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { int animatedValue = (int) animation.getAnimatedValue(); Log.i("szjanimatedValue",animatedValue+""); //Set value to TextView object tv.layout(animatedValue,animatedValue,tv.getWidth()+animatedValue,tv.getHeight()+animatedValue); } }); valueAnimator.start();
As you can see, the pop-up Log starts from 0 - > 400 - > 200 - > 600
Attribute animation ValueAnimator.onFloat use
Parametric structure:
ValueAnimator.ofFloat(float... values)
This is basically similar to the use of ofInt, except that the parameters passed are different. onInt is of type int and onFloat is of type float,
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 400f,200f,600f,405.2f,202.1f,600f); valueAnimator.setDuration(2000); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //Because we use of Float here, we have to convert it to Float Float animated = (Float) animation.getAnimatedValue(); Log.i("szjanimatedValue",animated+""); int animatedValue = animated.intValue();//Convert to int type set to tv tv.layout(animatedValue,animatedValue,tv.getWidth()+animatedValue,tv.getHeight()+animatedValue); } }); valueAnimator.start();
Come to Kangkang onFloat's effect:
Summary of onInt and onFloat:
Common methods:
Animation duration in milliseconds ValueAnimator setDuration(long duration) Gets the value of the current motion point when ValueAnimator is in motion Object getAnimatedValue(); Start animation void start() Set the number of cycles to INFINITE void setRepeatCount(int value) *Set cycle mode *value values include RESTART and REVERSE void setRepeatMode(int value) Cancel animation void cancel()
setRepeatCount() and setRepeatMode() use
Here I set:
valueAnimator.setRepeatCount(3) ; / / execute three times
valueAnimator.setRepeatMode ( ValueAnimator.REVERSE ); / / reverse
Listener method
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //Gets the value of the current motion point when ValueAnimator is in motion Object animated =animation.getAnimatedValue(); });
animation.getAnimatedValue();//Gets the value of the current motion point when ValueAnimator is in motion
valueAnimator.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { //Start with Log.i("szjjListener:","start"); } @Override public void onAnimationEnd(Animator animation) { //Execute after execution Log.i("szjjListener","End"); } @Override public void onAnimationCancel(Animator animation) { Log.i("szjjListener","Cancel"); } @Override public void onAnimationRepeat(Animator animation) { //Not once executed after start Log.i("szjjListener","Repeat"); } });
because valueAnimator.setRepeatCount(3) ; / / execute three times
So here onaimationrepeat() is executed three times
Cancel listening
/** * Remove AnimatorUpdateListener */ void removeUpdateListener(AnimatorUpdateListener listener); void removeAllUpdateListeners(); /** * Remove AnimatorListener */ void removeListener(AnimatorListener listener); void removeAllListeners();
valueAnimator.removeAllListeners();
effect:
Take a look at the Log:
As can be seen from the Log, as long as the valueAnimator.removeAllListeners(); it will be stopped immediately and will not be executed further. Under normal circumstances, it will be 0f - > 400 - > 200F - > 600F - > 405.2f - > 202.1f - > 600F
Clone and delay operation
How long does the delay start, in milliseconds public void setStartDelay(long startDelay) Clone a ValueAnimator instance completely, including all its settings and all processing of listener code public ValueAnimator clone()
use:
Note: you must have a valueAnimator control before you can clone. Otherwise, it will be null
ValueAnimator clone = valueAnimator.clone();//clone clone.setStartDelay(2000);//Execute after 2s clone.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //Because we use of Float here, we have to convert it to Float Float animated = (Float) animation.getAnimatedValue(); Log.i("szjanimatedValue",animated+""); int animatedValue = animated.intValue();//Convert to int type set to tv tv.layout(animatedValue,animatedValue,tv.getWidth()+animatedValue,tv.getHeight()+animatedValue); } }); clone.start();
Come to Kangkang effect:
First get the original control, and then click clone
Thank you for watching. If you have different opinions, please leave a message in the comment area~