Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動畫插值器 Interpolator 全解析

編輯:關於 Android 程式設計

備註:在 AnimationSet 中,Scale 應該比 Translate 更早加入。

Interpolator 是時間插值類,用來定義動畫變換的速度曲線,能為 alpha/scale/translate/rotate 動畫實現加速、減速、反彈等效果。Interpolator 本身其實是一個空介面,繼承自 TimeInterpolator。TimeInterpolator 允許動畫做非線性運動變換,介面中唯一的方法是 float getInterpolation(float input):傳入值是一個 0.0 到 1.0 之間的浮點數,返回值則可以小於 0.0,也可以大於 1.0——這正是「反彈」類效果能成立的關鍵。

常用派生類

  1. AccelerateDecelerateInterpolator:開頭與結尾變化較慢,中間加速。
  2. AccelerateInterpolator:開頭較慢,隨後持續加速。
  3. AnticipateInterpolator:先向反方向退一步,再向前甩出。
  4. AnticipateOvershootInterpolator:先向後退,再向前超出一定量後回到終值。
  5. BounceInterpolator:動畫結束時像皮球一樣彈跳。
  6. CycleInterpolator:按正弦曲線循環播放指定次數。
  7. DecelerateInterpolator:開頭快、結尾慢。
  8. LinearInterpolator:等速變化。
  9. OvershootInterpolator:向前甩出一段距離後再回到原位。
  10. PathInterpolator:較晚加入的一員,可以自訂路徑座標,讓動畫沿著路徑跑。注意這裡的座標是單方向的時間軸,例如從 0 到 1、回彈到 0.5、再到 0.7、再到 0.3,直到時間結束。

源碼選讀

LinearInterpolator:直接回傳輸入值

/**
 * An interpolator where the rate of change is constant
 */
@HasNativeInterpolator
public class LinearInterpolator implements Interpolator, NativeInterpolatorFactory {
    public LinearInterpolator() {}

    public LinearInterpolator(Context context, AttributeSet attrs) {}

    public float getInterpolation(float input) {
        return input;
    }

    /** @hide */
    @Override
    public long createNativeInterpolator() {
        return NativeInterpolatorFactoryHelper.createLinearInterpolator();
    }
}

DecelerateInterpolator

它可以透過 XML 設定屬性,其中 mFactor 預設為 1.0,值越大變化越劇烈:起點更快,終點更慢。getInterpolation() 描述的其實是一條拋物線。

@HasNativeInterpolator
public class DecelerateInterpolator implements Interpolator, NativeInterpolatorFactory {
    private float mFactor = 1.0f;

    public DecelerateInterpolator() {}

    /**
     * @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 exaggerates the ease-out effect.
     */
    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;
    }

    /** @hide */
    @Override
    public long createNativeInterpolator() {
        return NativeInterpolatorFactoryHelper.createDecelerateInterpolator(mFactor);
    }
}

自己動手寫一個

網路上流傳著一套很經典的 easing 公式,以正弦為例:

public class Sine {
    public static float easeIn(float t, float b, float c, float d) {
        return -c * (float) Math.cos(t / d * (Math.PI / 2)) + c + b;
    }
    public static float easeOut(float t, float b, float c, float d) {
        return c * (float) Math.sin(t / d * (Math.PI / 2)) + b;
    }
    public static float easeInOut(float t, float b, float c, float d) {
        return -c / 2 * ((float) Math.cos(Math.PI * t / d) - 1) + b;
    }
}

問題是,這四個參數(當前時間 t、起始值 b、變化量 c、總時長 d)該怎麼跟 Interpolator 的 getInterpolation(float) 對接?

做法很簡單:把總時長 d 固定為 1.0,起始值 b 設為 0.0,結束值設為 1.0(於是變化量 c 也就是 1.0),再把 Interpolator 傳入的 input 當作 t 丟進去,就完全對上了。

一個實際案例

/**
 * Created by Qiujuer on 2015/1/5.
 */
public class InSineInterpolator implements Interpolator {
    public static float easeIn(float t, float b, float c, float d) {
        return -c * (float) Math.cos(t / d * (Math.PI / 2)) + c + b;
    }

    @Override
    public float getInterpolation(float input) {
        return easeIn(input, 0, 1, 1);
    }
}

怎麼用

mAnimatorSet.playTogether(aPaintX, aPaintY, aRadius, aBackground);
mAnimatorSet.setInterpolator(new InSineInterpolator());
mAnimatorSet.start();

使用方式和系統自帶的插值器完全一樣。這裡只是個示範,實務中你可以自由封裝,前提是不改動核心的計算邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved