Interpolator in Android

Source: Internet
Author: User

Interpolator is used for time interpolation in animations, which is the function of mapping 0 to 1 of floating-point values to another floating-point value change.

This article lists several implementations of the interpolator provided by the Android API, lists the source code, and draws its mathematical curve with a program. (The project link is attached to the text).

Android:interpolator

Interpolator is used to modify the animation effect, define the rate of change of the animation, can make the existing animation effect accelerated (acceleration), decelerated (deceleration), repeated (repetition), bounced (bounce) and so on.

The contents of the document in Android are as follows:

Acceleratedecelerateinterpolator rate changes at the beginning and end of the animation are slower, accelerating in the middle

Accelerateinterpolator at the beginning of the animation the rate change is slow, and then start to accelerate

Anticipateinterpolator starts back and then moves forward.

Anticipateovershootinterpolator starts back and then dumps a certain value and returns the last value.

Bounceinterpolator animation at the end of the play

Cycleinterpolator Animation Loop plays a specific number of times, rate changes along the sine curve

Decelerateinterpolator in the beginning of the animation and then slowly

Linearinterpolator change at constant rate

Overshootinterpolator forward a certain value before returning to its original position.

If Android-defined interpolators does not match your effect, you can also customize Interpolators

Acceleratedecelerateinterpolator
/** * An interpolator where the rate of change starts and ends slowly but * accelerates through the middle. *  */public class Acceleratedecelerateinterpolator implements Interpolator {    public Acceleratedecelerateinterpolator () {    }        @SuppressWarnings ({"Unuseddeclaration"})    Public Acceleratedecelerateinterpolator (context context, AttributeSet attrs) {    } public        float getinterpolation ( Float input) {        return (float) (Math.Cos ((input + 1) * Math.PI)/2.0f) + 0.5f;}    }

Accelerateinterpolator
/** * An interpolator where the rate of change starts out slowly and and then accelerates.    * */public class Accelerateinterpolator implements Interpolator {private final float mfactor;    Private final double mdoublefactor;        Public Accelerateinterpolator () {mfactor = 1.0f;    Mdoublefactor = 2.0; }/** * Constructor * * @param factor degree to which the animation should is eased. Seting * Factor to 1.0f produces a y=x^2 parabola. Increasing factor above * 1.0f exaggerates the ease-in effect (i.e., it starts even * slower and EN        DS evens faster) */public Accelerateinterpolator (float factor) {mfactor = factor;    Mdoublefactor = 2 * mfactor; } public Accelerateinterpolator (context context, AttributeSet attrs) {TypedArray a = Context.obt                Ainstyledattributes (Attrs, com.android.internal.r.styleable.accelerateinterpolator); Mfactor = A.getfloat (Com.androId.internal.r.styleable.accelerateinterpolator_factor, 1.0f);        Mdoublefactor = 2 * mfactor;    A.recycle ();        public float getinterpolation (float input) {if (Mfactor = = 1.0f) {return input * input;        } else {return (float) Math.pow (input, mdoublefactor); }    }}

Anticipateinterpolator
/** * An interpolator where the change is starts backward then flings forward.    */public class Anticipateinterpolator implements Interpolator {private final float mtension;    Public Anticipateinterpolator () {mtension = 2.0f; }/** * @param tension Amount of anticipation.                When tension equals 0.0f, there are * no anticipation and the interpolator becomes a simple *     Acceleration Interpolator.    */Public Anticipateinterpolator (float tension) {mtension = tension; } public Anticipateinterpolator (context context, AttributeSet attrs) {TypedArray a = Context.obtainstyledattrib        Utes (Attrs, com.android.internal.r.styleable.anticipateinterpolator);        Mtension = A.getfloat (com.android.internal.r.styleable.anticipateinterpolator_tension, 2.0f);    A.recycle (); } public float getinterpolation (float t) {//A (t) = t * t * ((tension + 1) * t-tension) return T * T * ((mtension + 1) * t-mtension); }}

Anticipateovershootinterpolator
/** * An interpolator where the change starts backward then flings forward and overshoots * The target value and finally g OES back to the final value.    */public class Anticipateovershootinterpolator implements Interpolator {private final float mtension;    Public Anticipateovershootinterpolator () {mtension = 2.0f * 1.5f; }/** * @param tension Amount of Anticipation/overshoot.                When tension equals 0.0f, * There is no anticipation/overshoot and the interpolator becomes *     A simple acceleration/deceleration interpolator.    */Public Anticipateovershootinterpolator (float tension) {mtension = tension * 1.5f; }/** * @param tension Amount of Anticipation/overshoot.                When tension equals 0.0f, * There is no anticipation/overshoot and the interpolator becomes *     A simple acceleration/deceleration interpolator. * @param extratension Amount by which to multiply the tension. For instance, * to get the same overshoot as a overshootinterpolator with *     A tension of 2.0f, you would use an extratension of 1.5f. */Public Anticipateovershootinterpolator (float tension, float extratension) {mtension = tension * extratension    ; } public Anticipateovershootinterpolator (context context, AttributeSet attrs) {TypedArray a = Context.obtainsty        Ledattributes (Attrs, anticipateovershootinterpolator); Mtension = A.getfloat (anticipateovershootinterpolator_tension, 2.0f) * A.getfloat (Anticipateovershootinterp        Olator_extratension, 1.5f);    A.recycle ();    } private static float A (float T, float s) {return T * t * ((s + 1) * t-s);    } private static float O (float t, float s) {return T * t * ((s + 1) * t + s); } public float getinterpolation (float t) {//A (t, s) = t * t * ((s + 1) * t-s)//O (t, s) = t * t * (( S + 1) * t + s)//F (t) = 0.5 * A (T * 2, tension * extratension), when T < 0.5//f (t) = 0.5 * (o (t * 2-2, tension * extratension        ) + 2), when T <= 1.0 if (T < 0.5f) return 0.5f * A (T * 2.0f, mtension);    else return 0.5f * (O (t * 2.0f-2.0f, Mtension) + 2.0f); }}

Bounceinterpolator
/** * An interpolator where the change is bounces at the end. */public class Bounceinterpolator implements Interpolator {public    bounceinterpolator () {    }    @ Suppresswarnings ({"Unuseddeclaration"}) public    Bounceinterpolator (context context, AttributeSet attrs) {    }    private static float bounce (float t) {        return T * t * 8.0f;    }    public float getinterpolation (float t) {        //_b (t) = t * t * 8        //BS (t) = _b (t) for T < 0.3535        //bs (t) = _b (t-0.54719) + 0.7 for T < 0.7408        //bs (t) = _b (t-0.8526) + 0.9 for T < 0.9644        //bs (t) = _b (t-1.04 + 0.95 for T <= 1.0        //b (t) = BS (T * 1.1226)        t *= 1.1226f;        if (T < 0.3535f) return Bounce (t);        else if (T < 0.7408f) return Bounce (t-0.54719f) + 0.7f;        else if (T < 0.9644f) return Bounce (t-0.8526f) + 0.9f;        else return Bounce (t-1.0435f) + 0.95f;    }}

Cycleinterpolator
/** * Repeats The animation for a specified number of cycles. The * Rate of change follows a sinusoidal pattern. * */public class Cycleinterpolator implements Interpolator {public    cycleinterpolator (float cycles) {        Mcycles = C ycles;    }        Public Cycleinterpolator (context context, AttributeSet attrs) {        TypedArray a =            context.obtainstyledattributes (Attrs, com.android.internal.r.styleable.cycleinterpolator);                Mcycles = A.getfloat (Com.android.internal.r.styleable.cycleinterpolator_cycles, 1.0f);                A.recycle ();    }        public float getinterpolation (float input) {        return (float) (Math.sin (2 * mcycles * Math.PI * input));        private float Mcycles;}

A curve with a 2 o'clock parameter:

Decelerateinterpolator
/** * An interpolator where the rate of change starts out quickly and and then decelerates. * */public class Decelerateinterpolator implements Interpolator {public decelerateinterpolator () {}/** * C Onstructor * * @param factor degree to which the animation should is 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 even    s slower) */public Decelerateinterpolator (float factor) {mfactor = factor; } public Decelerateinterpolator (context context, AttributeSet attrs) {TypedArray a = Context.obt                Ainstyledattributes (Attrs, com.android.internal.r.styleable.decelerateinterpolator);                Mfactor = A.getfloat (Com.android.internal.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;}

Linearinterpolator
/** * An interpolator where the rate of change is constant * */public class Linearinterpolator implements Interpolator {
   public Linearinterpolator () {    } public        Linearinterpolator (context context, AttributeSet attrs) {    } Public        float getinterpolation (float input) {        return input;    }}

Overshootinterpolator
/** * An interpolator where the change flings forward and overshoots the last value * then comes back.    */public class Overshootinterpolator implements Interpolator {private final float mtension;    Public Overshootinterpolator () {mtension = 2.0f; }/** * @param tension Amount of overshoot.                When tension equals 0.0f, there are * no overshoot and the interpolator becomes a simple *     Deceleration interpolator.    */Public Overshootinterpolator (float tension) {mtension = tension; } public Overshootinterpolator (context context, AttributeSet attrs) {TypedArray a = Context.obtainstyledattribu        TES (Attrs, Com.android.internal.r.styleable.overshootinterpolator);        Mtension = A.getfloat (com.android.internal.r.styleable.overshootinterpolator_tension, 2.0f);    A.recycle ();  } public float getinterpolation (float t) {//_o (t) = t * t * ((tension + 1) * t + tension)      O (t) = _o (t-1) + 1 T-= 1.0f;    return T * t * ((mtension + 1) * t + mtension) + 1.0f; }}

Project Link:

Https://github.com/mengdd/HelloInterpolator.git

Interpolator in Android

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.