Interpolator time interpolation class defines the speed of animation transformation. Acceleration, deceleration and repetition of alpha/scale/translate/rotate animation can be achieved. The Interpolator class is actually an empty interface inherited from TimeInterpolator, which allows animation to perform non-linear motion transformation, such as acceleration and speed limit. There is only one method float in the interface. getInterpolation(float input) method. The incoming value is a value of 0.0 to 1.0, and the return value can be less than 0.0 or greater than 1.0.
Common inheritance classes
- Accelerate Decelerate Interpolator================================= Animation starts and ends at a slower rate change, accelerating in the middle.
- Accelerate Interpolator================================================= where the animation starts, the rate changes slowly and then starts to accelerate.
- AnticipateInterpolator ============================================== Starts backward and then flings forward.
- Anticipate Overshoot Interpolator================================ Starts backward and then throws forward a certain value and returns the final value.
- BounceInterpolator=============================================== pops up at the end of the animation.
- Cycle Interpolator========================================== The number of times the animation is played in a loop, and the rate changes along the sinusoidal curve.
- Decelerate Interpolator================================================== Fast and slow at the beginning of the animation.
- Linear Interpolator================================================ changes at a constant rate.
- Overshoot Interpolator================================================= Throw forward a certain value and return to the original position.
- Path Interpolator================================================================= New, that is, you can define the path coordinates and then run according to the path coordinates; note that the coordinates are not XY, but one-way, that is, I can bounce back from 0 to 1, then 0.5 to 0.7 to 0.3, until the end of the time.
Several commonly used interpolator source codes:
Linear Interpolator Linear Interpolator, which returns the input value directly.
DecelerateInterpolator
Animation attributes can be set by XML, and mFactor variables can be set by XML. The default value is 1.0; the larger the value, the faster it changes; the result is that it starts faster, and the result is slower. getInterpolation(float) describes a parabolic equation in a junior high school.
Version implementation of C++:
Meaning of the parameter:
- t - Current time in animation
- b - Start Value
- c - End Value
- d - Total time of animation
Although there are Java, but how to use this ah, with the above Interpolator how to connect ah?
A simple method: first, set the total time of d to a fixed value of 1.0, the start value of b to 0.0, and the end value to 1.0, then t as float get Interpolation (float input) in the above Interpolator; pass in the value, then you can not use it.
Take a Case.
Use
It can be seen that the use of Android is exactly the same as that of Android. Of course, this is only a Case. You can encapsulate it freely in the specific use, provided that you don't change the main part.
- /**
- * An interpolator where the rate of change starts out quickly and
- * and then decelerates.
- *
- */
- @HasNativeInterpolator
- public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
- public DecelerateInterpolator() {
- }
- /**
- * Constructor
- *
- * @param factor Degree to which the animation should be eased. Setting factor to 1.0f produces
- * an upside-down y=x^2 parabola. Increasing factor above 1.0f makes exaggerates the
- * ease-out effect (i.e., it starts even faster and ends evens slower)
- */
- public DecelerateInterpolator(float factor) {
- mFactor = factor;
- }
- public DecelerateInterpolator(Context context, AttributeSet attrs) {
- this(context.getResources(), context.getTheme(), attrs);
- }
- /** @hide */
- public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
- TypedArray a;
- if (theme != null) {
- a = theme.obtainStyledAttributes(attrs, R.styleable.DecelerateInterpolator, 0, 0);
- } else {
- a = res.obtainAttributes(attrs, R.styleable.DecelerateInterpolator);
- }
- mFactor = a.getFloat(R.styleable.DecelerateInterpolator_factor, 1.0f);
- a.recycle();
- }
- public float getInterpolation(float input) {
- float result;
- if (mFactor == 1.0f) {
- result = (float)(1.0f - (1.0f - input) * (1.0f - input));
- } else {
- result = (float)(1.0f - Math.pow((1.0f - input), 2 * mFactor));
- }
- return result;
- }
- private float mFactor = 1.0f;
- /** @hide */
- @Override
- public long createNativeInterpolator() {
- return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
- }
- }