Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

深入解讀 Android 動畫 Interpolator 的運作原理

編輯:Android 資訊

做過 Android 動畫的人對 Interpolator 應該不陌生。它的職責是控制動畫的執行速率。默認情況下,動畫並不是等速的——系統給的默認節奏是先加速再減速。

還記得上篇用屬性動畫做的旋轉效果嗎?沒設 Interpolator 時,它就是先快後慢。現在換成系統提供的 LinearInterpolator,動畫便會變成等速。先看個對照實驗:兩個 TextView 疊放,點擊後同時旋轉,其中一個設了 LinearInterpolator,另一個什麼都不設。

LinearInterpolator ll = new LinearInterpolator();

ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f);
animator.setInterpolator(ll);
animator.setDuration(5000);
animator.start();

ObjectAnimator animator2 = ObjectAnimator.ofFloat(tv2, "rotation", 0f, 360f);
animator2.setDuration(5000);
animator2.start();

差異一目了然:一個先加速後減速,一個全程勻速。

於是問題來了——LinearInterpolator 到底做了什麼?我們去翻源碼。

setInterpolator 背後

調用 animator.setInterpolator(ll) 時,實際走的是 ValueAnimator 的這個方法:

public void setInterpolator(TimeInterpolator value) {
    if (value != null) {
        mInterpolator = value;
    } else {
        mInterpolator = new LinearInterpolator();
    }
}

注意這裡有個 mInterpolator 成員。那如果我們完全不調這個方法,它的默認值是什麼?搜索一下:

// The time interpolator to be used if none is set on the animation
private static final TimeInterpolator sDefaultInterpolator = new AccelerateDecelerateInterpolator();
private TimeInterpolator mInterpolator = sDefaultInterpolator;

真相浮現:默認用的是 AccelerateDecelerateInterpolator。繼續往下挖:

public class AccelerateDecelerateInterpolator extends BaseInterpolator
        implements NativeInterpolatorFactory {

    public AccelerateDecelerateInterpolator() {
    }

    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();
    }
}

核心就是 getInterpolation,裡面用了餘弦函數。傳入的 input 落在 0 到 1 之間,輸出值的增長率先升後降——對應到動畫上,就是先加速後減速。有興趣可以用 MATLAB 把這條曲線畫出來看看。

反觀 LinearInterpolator:

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();
    }
}

乾脆俐落,原樣返回 input,不做任何加工。input 本身是均勻遞增的,動畫自然勻速。

看到這裡,猜想就成立了:想自定義節奏,覆寫 getInterpolation 即可。

繼承結構與那個終極介面

我們日常用的各種 Interpolator 都繼承自 Interpolator,而 Interpolator 實現了 TimeInterpolator 介面:

那麼這位「終極 Boss」長什麼樣?

package android.animation;

/**
 * A time interpolator defines the rate of change of an animation.
 * This allows animations to have non-linear motion, such as
 * acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * Maps a value representing the elapsed fraction of an animation to a
     * value that represents the interpolated fraction.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *              in the animation where 0 represents the start and 1.0
     *              represents the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);
}

源碼簡單到只有一個方法。注意文檔裡那句話:返回值可以大於 1.0(衝過頭),也可以小於 0(沒到位)——這正是各類回弹效果的實現原理。

動手寫一個自定義 Interpolator

既然介面只有一個方法,那自定義就毫無門檻:

public class TanInterpolator implements TimeInterpolator {
    @Override
    public float getInterpolation(float t) {
        return (float) Math.sin((t / 2) * Math.PI);
    }
}

掛上去用:

TanInterpolator tl = new TanInterpolator();
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "rotation", 0f, 360f);
animator.setInterpolator(tl);
animator.setDuration(5000);
animator.start();

效果是什麼?一開始速度很快,然後逐漸減弱到 0。

原因看正弦曲線就明白了:

x 取 0 到 0.5π 這一段,y 從 0 升到 1,而這段曲線的斜率不斷變小直至為 0——對應到動畫上,就是起步飛快、尾聲緩停。

把這層關係看透,你之後想調出任何手感,都只是換一條數學曲線的事。

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