Android animation interpolator

摘要:Android 動畫

有關詳細的interpolator可參考此連結:
http://cogitolearning.co.uk/?p=1078

測試範例是從Android demo sample來了解各種interpolator變化

Android所提供的各種interpolator:

AccelerateDecelerateInterolator
先加速後減速,開始結束時慢,中間加速
    public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;  
        // y = cos((t + 1)Math.PI)/2 + 0.5
    }

 

 
AccelerateInterpolator
加速,開始時慢中間加速
    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }

 

 
DecelerateInterpolator
減速,開始時快然後減速
    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));
            // y = (1 - (1 - t)^2f)
        }
        return result;
    }

 

 
AnticipateInterpolator
反向 ,先向相反方向改變一段再加速播放
    public float getInterpolation(float t) {
        // a(t) = t * t * ((tension + 1) * t - tension)
        return t * t * ((mTension + 1) * t - mTension);  
        // y = (T+1)*t^3 - T*t^2
    }

 

 
AnticipateOvershootInterpolator
反向加超越,先向相反方向改變,再加速播放,會超出目的值然後緩慢移動至目的值
    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
跳躍,快到目的值時值會跳躍,如目的值100,後面的值可能依次為85,77,70,80,90,100
    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;
    }

 

 
CycleIinterpolator
迴圈,動畫迴圈一定次數,值的改變為一正弦函數
    public float getInterpolation(float input) {
        return (float)(Math.sin(2 * mCycles * Math.PI * input));
    }

 

 
LinearInterpolator
線性,線性均勻改變
    public float getInterpolation(float input) {
        return input;  // y = t
    }

 

 
OvershottInterpolator
超越,最後超出目的值然後緩慢改變到目的值
    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;
    }

 

 
TimeInterpolator
一個介面,允許你自訂interpolator,以上幾個都是實現了這個介面
 

修改google api-demo的animation部分:

測試一:
(1) No Interpolator, (2) LinearInterpolator, (3) AccelerateInterpolator, (4) DecelerateInterpolator, (5) AccelerateDecelerateInterolator

 

測試二:
(1) No Interpolator, (2) AnticipateInterpolator, (3) AnticipateOvershootInterpolator, (4) BounceInterpolator, (5) CycleIinterpolator