Android animation of the people should not be unfamiliar with the interpolator, this class is mainly used to control the speed of the Android animation, in general, if we do not set, the animation is not a uniform execution, the system by default is to speed up and then slow down such an animation execution rate.
Android uses the Interpolator class to let us control the rate of animation execution, remember the rotation effect we implemented using property animations in the previous blog? In the case where Interpolator is not set, this animation is accelerated and then slowed down, we now use the class Linearinterpolator provided by the Android system to set the rate of execution of the animation, Linearinterpolator can make this animation uniform execution , let's take a look at a case where we have two textview overlapping, and after clicking the Rotate button these two textview simultaneously perform the rotation animation, the difference is a set linearinterpolator, and the other one is not set, the code is as follows:
Linearinterpolator ll = new Linearinterpolator ();Objectanimator animator = Objectanimator. Offloat(TV,"Rotation",0F theF;Animator. Setinterpolator(LL);Animator. Setduration( the);Animator. Start();Objectanimator Animator2 = Objectanimator. Offloat(TV2,"Rotation",0F theF;Animator2. Setduration( the);Animator2. Start();
As follows:
Now we can clearly see the difference here, a textview acceleration after deceleration, one has been uniform motion.
This aroused my curiosity, exactly what Linearinterpolator did and changed the rate of execution of the animation. Here we will look at the source code.
When we call animator.setInterpolator(ll); , the call is the Valueanimator method in the Setinterpolator method, the source code is as follows:
publicvoidsetInterpolatorvalue) { if (valuenull) { value; else { new LinearInterpolator(); } }
We see that there is a minterpolator variable, and if we do not execute this method, then what is the default value of Minterpolator?
We found two lines of code:
// The time interpolator to be used if none is set on the animation privatestaticfinal TimeInterpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator();private TimeInterpolator mInterpolator = sDefaultInterpolator;
This is clear, if we do not set, then the system uses the default Acceleratedecelerateinterpolator,acceleratedecelerateinterpolator what is it? Continue to see the source code:
Public class acceleratedecelerateinterpolator extends baseinterpolator Implements nativeinterpolatorfactory { Public Acceleratedecelerateinterpolator() { }@SuppressWarnings({"Unuseddeclaration"}) Public Acceleratedecelerateinterpolator(context context, AttributeSet attrs) { } Public float getinterpolation(floatInput) {return(float) (Math.Cos (input +1) * Math.PI)/2.0f) +0.5F }/** @hide * / @Override Public Long Createnativeinterpolator() {returnNativeinterpolatorfactoryhelper.createacceleratedecelerateinterpolator (); }}
One of the core functions here is getinterpolation, using the inverse cosine function, input value is passed between 0-1, so here the rate of change of the return value is increased first and then decreased, the corresponding animation execution rate is increased first and then slowed down. Interested children's shoes you can use MATLAB to draw the image of this function. And when we realized the Linearinterpolator, things changed:
Public class linearinterpolator extends baseinterpolator implements Nativeinterpolatorfactory { Public Linearinterpolator() { } Public Linearinterpolator(context context, AttributeSet attrs) { } Public float getinterpolation(floatInput) {returnInput }/** @hide * / @Override Public Long Createnativeinterpolator() {returnNativeinterpolatorfactoryhelper.createlinearinterpolator (); }}
This is a neat and straightforward return to input, without any calculations. Input returns a uniform value, so the animation is executed at a constant speed.
See here, everyone should understand, if we want to control the animation execution rate, should rewrite the Getinterpolation method can be implemented. In order to confirm our conjecture, we continue to look at the source code.
Most of the time, the systems we use provide a wide variety of **interpolator, such as the above mentioned Linearinterpolator, which are inherited from Interpolator, While Interpolator implements the Timeinterpolator interface, let's look at an inheritance structure diagram:
So what exactly is this ultimate big Boss timeinterpolator like?
Package android.animation;/** * A TimeInterpolator defines theRate ofChange ofan animation. This allows animations * toHave non-linear motion, such asAcceleration andDeceleration. */public interface Timeinterpolator {/** * Maps a value representing theElapsed fraction ofAn animation toA value thatRepresents * theInterpolated fraction. This interpolated value is ThenMultiplied by theChangeinch* Value ofAn animation toDerive theAnimated value at theCurrent elapsed animation Time. * * @param input A valuebetween 0 and 1.0indicating our current point *inch theAnimationwhere 0Represents theStart and 1.0Represents * the End* @returnThe interpolation value. This value can is more than1.0 for* Interpolators which overshoot their targets,or Less than 0 for* Interpolators thatUndershoot their targets. */float Getinterpolation (float input);}
Source is still very simple, there is only one way, is getinterpolation, it seems right, it is, if we want to customize the Interpolator, Just implement the Getinterpolation method of the Timeinterpolator interface, and the Getinterpolation method receives the parameter as a percentage of the animation execution, which is uniform.
Let's take a simple case:
publicclass TanInterpolator implements TimeInterpolator { @Override publicfloatgetInterpolation(float t) { return (float2) * Math.PI); }}
Use in animations:
TanInterpolator tl = new TanInterpolator(); ObjectAnimator animator = ObjectAnimator.ofFloat"rotation", 0360f); animator.setInterpolator(tl); animator.setDuration(5000); animator.start();
Hey? What effect is this? This is the beginning of a very high speed, and then gradually reduced to 0 animation effect.
The reasons are as follows:
Look, this is the SIN function image:
X takes a 0-0.5pi,y value of 0-1, and the slope of the curve is gradually reduced to 0, which is why our animation begins to execute very quickly, and then the speed gradually becomes 0.
Well, after reading these, you must have understood the use of this class.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.
Understand the use of Android animation Interpolator class from the source point of view