Android中的Interpolator

來源:互聯網
上載者:User

Android中的Interpolator

 

系統提供的InterpolatorLinearInterpolator 線性插值器


public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {    public LinearInterpolator() {    }    public LinearInterpolator(Context context, AttributeSet attrs) {    }    public float getInterpolation(float input) {        return input;    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createLinearInterpolator();    }}
AccelerateInterpolator 加速插值器



原始碼:

public class AccelerateDecelerateInterpolator extends BaseInterpolator        implements NativeInterpolatorFactory {    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;    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createAccelerateDecelerateInterpolator();    }}
減速DecelerateInterpolator



源碼:

/** * An interpolator where the rate of change starts out quickly and * and then decelerates. * */@HasNativeInterpolatorpublic class DecelerateInterpolator extends BaseInterpolator implements 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);        setChangingConfiguration(a.getChangingConfigurations());        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);    }}
AccelerateDecelerateInterpolator 加速減速插值器



源碼:

@HasNativeInterpolatorpublic class AccelerateDecelerateInterpolator extends BaseInterpolator        implements NativeInterpolatorFactory {    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;    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createAccelerateDecelerateInterpolator();    }}
BounceInterpolator 彈跳插值器


package android.view.animation;import android.content.Context;import android.util.AttributeSet;import com.android.internal.view.animation.HasNativeInterpolator;import com.android.internal.view.animation.NativeInterpolatorFactory;import com.android.internal.view.animation.NativeInterpolatorFactoryHelper;/** * An interpolator where the change bounces at the end. */@HasNativeInterpolatorpublic class BounceInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {    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.0435) + 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;    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createBounceInterpolator();    }}
AnticipateInterpolator 回蕩鞦韆插值器


public class AnticipateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {    private final float mTension;    public AnticipateInterpolator() {        mTension = 2.0f;    }    /**     * @param tension Amount of anticipation. When tension equals 0.0f, there is     *                no anticipation and the interpolator becomes a simple     *                acceleration interpolator.     */    public AnticipateInterpolator(float tension) {        mTension = tension;    }    public AnticipateInterpolator(Context context, AttributeSet attrs) {        this(context.getResources(), context.getTheme(), attrs);    }    /** @hide */    public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) {        TypedArray a;        if (theme != null) {            a = theme.obtainStyledAttributes(attrs, R.styleable.AnticipateInterpolator, 0, 0);        } else {            a = res.obtainAttributes(attrs, R.styleable.AnticipateInterpolator);        }        mTension = a.getFloat(R.styleable.AnticipateInterpolator_tension, 2.0f);        setChangingConfiguration(a.getChangingConfigurations());        a.recycle();    }    public float getInterpolation(float t) {        // a(t) = t * t * ((tension + 1) * t - tension)        return t * t * ((mTension + 1) * t - mTension);    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createAnticipateInterpolator(mTension);    }}
AnticipateOvershootInterpolator


/** * An interpolator where the change starts backward then flings forward and overshoots * the target value and finally goes back to the final value. */@HasNativeInterpolatorpublic class AnticipateOvershootInterpolator extends BaseInterpolator        implements NativeInterpolatorFactory {    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 an 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) {        this(context.getResources(), context.getTheme(), attrs);    }    /** @hide */    public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {        TypedArray a;        if (theme != null) {            a = theme.obtainStyledAttributes(attrs, AnticipateOvershootInterpolator, 0, 0);        } else {            a = res.obtainAttributes(attrs, AnticipateOvershootInterpolator);        }        mTension = a.getFloat(AnticipateOvershootInterpolator_tension, 2.0f) *                a.getFloat(AnticipateOvershootInterpolator_extraTension, 1.5f);        setChangingConfiguration(a.getChangingConfigurations());        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);    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createAnticipateOvershootInterpolator(mTension);    }}
CycleInterpolator 正弦周期變化插值器


 @HasNativeInterpolatorpublic class CycleInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {    public CycleInterpolator(float cycles) {        mCycles = cycles;    }    public CycleInterpolator(Context context, AttributeSet attrs) {        this(context.getResources(), context.getTheme(), attrs);    }    /** @hide */    public CycleInterpolator(Resources resources, Theme theme, AttributeSet attrs) {        TypedArray a;        if (theme != null) {            a = theme.obtainStyledAttributes(attrs, R.styleable.CycleInterpolator, 0, 0);        } else {            a = resources.obtainAttributes(attrs, R.styleable.CycleInterpolator);        }        mCycles = a.getFloat(R.styleable.CycleInterpolator_cycles, 1.0f);        setChangingConfiguration(a.getChangingConfigurations());        a.recycle();    }    public float getInterpolation(float input) {        return (float)(Math.sin(2 * mCycles * Math.PI * input));    }    private float mCycles;    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createCycleInterpolator(mCycles);    }}
OvershootInterpolator


/** * An interpolator where the change flings forward and overshoots the last value * then comes back. */@HasNativeInterpolatorpublic class OvershootInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {    private final float mTension;    public OvershootInterpolator() {        mTension = 2.0f;    }    /**     * @param tension Amount of overshoot. When tension equals 0.0f, there is     *                no overshoot and the interpolator becomes a simple     *                deceleration interpolator.     */    public OvershootInterpolator(float tension) {        mTension = tension;    }    public OvershootInterpolator(Context context, AttributeSet attrs) {        this(context.getResources(), context.getTheme(), attrs);    }    /** @hide */    public OvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {        TypedArray a;        if (theme != null) {            a = theme.obtainStyledAttributes(attrs, R.styleable.OvershootInterpolator, 0, 0);        } else {            a = res.obtainAttributes(attrs, R.styleable.OvershootInterpolator);        }        mTension = a.getFloat(R.styleable.OvershootInterpolator_tension, 2.0f);        setChangingConfiguration(a.getChangingConfigurations());        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;    }    /** @hide */    @Override    public long createNativeInterpolator() {        return NativeInterpolatorFactoryHelper.createOvershootInterpolator(mTension);    }}
自訂Interpolator

Interpolator源碼:

package android.view.animation;import android.animation.TimeInterpolator;/** * An interpolator defines the rate of change of an animation. This allows * the basic animation effects (alpha, scale, translate, rotate) to be  * accelerated, decelerated, repeated, etc. */public interface Interpolator extends TimeInterpolator {    // A new interface, TimeInterpolator, was introduced for the new android.animation    // package. This older Interpolator interface extends TimeInterpolator so that users of    // the new Animator-based animations can use either the old Interpolator implementations or    // new classes that implement TimeInterpolator directly.}

我們來寫自己的類來實現該介面

import android.view.animation.Interpolator;public class MyInterpolator implements Interpolator {    private float mFactor;    private int i;    public MyInterpolator(int i){        this.i = i;    }    @Override    public float getInterpolation(float input) { //定義我們的函數,input: 0 ~ 1        switch(i){            case 1:mFactor = input;                break;            case 2:mFactor = input*input*input;                break;        }        return mFactor;    }}

來個複雜的:

用desmos畫一個:

@Overridepublic float getInterpolation(float t) { //定義我們的函數,t: 0 ~ 1    if(t<0.2094) return (float)(-34*(t-0.18)*(t-0.18)+1.08);    else if(t <0.404) return (float)(5.9*(t-0.34)*(t-0.34)+0.95);    else if(t < 0.6045) return (float)(-3*(t-0.53)*(t-0.53)+1.02);    else if(t < 0.8064) return (float)((t-0.72)*(t-0.72)+0.99);    else return (float)(-0.3*(t-0.915)*(t-0.915)+1.001);    return mFactor;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.