Android 動(dòng)畫(huà)總結(jié)(4) - 插值器

Android 動(dòng)畫(huà)總結(jié)(1) - 概述
Android 動(dòng)畫(huà)總結(jié)(2) - 幀動(dòng)畫(huà)
Android 動(dòng)畫(huà)總結(jié)(3) - 補(bǔ)間動(dòng)畫(huà)
Android 動(dòng)畫(huà)總結(jié)(5) - 屬性動(dòng)畫(huà)
Android 動(dòng)畫(huà)總結(jié)(6) - 估值器
Android 動(dòng)畫(huà)總結(jié)(7) - ViewGroup 子元素間的動(dòng)畫(huà)
Android 動(dòng)畫(huà)總結(jié)(8) - Activity 轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
Android 動(dòng)畫(huà)總結(jié)(9) - 過(guò)渡動(dòng)畫(huà)


Interpolator 插值器继低,作用就是把 0 到 1 的浮點(diǎn)值變化映射到另一個(gè)浮點(diǎn)值變化熬苍,即根據(jù)時(shí)間流逝百分比計(jì)算出動(dòng)畫(huà)變化百分比。

圖片切線就是速度郁季。

AccelerateDecelerateInterpolator

public class AccelerateDecelerateInterpolator extends BaseInterpolator
        implements NativeInterpolatorFactory {

    public float getInterpolation(float input) {
        return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
    }

}
AccelerateDecelerateInterpolator.png

AccelerateInterpolator

public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
    private final float mFactor;
    private final double mDoubleFactor;

    public AccelerateInterpolator() {
        mFactor = 1.0f;
        mDoubleFactor = 2.0;
    }

    public AccelerateInterpolator(float factor) {
        mFactor = factor;
        mDoubleFactor = 2 * mFactor;
    }

    public AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

        mFactor = a.getFloat(R.styleable.AccelerateInterpolator_factor, 1.0f);
        mDoubleFactor = 2 * mFactor;
        setChangingConfiguration(a.getChangingConfigurations());
        a.recycle();
    }

    public float getInterpolation(float input) {
        if (mFactor == 1.0f) {
            return input * input;
        } else {
            return (float)Math.pow(input, mDoubleFactor);
        }
    }

}
AccelerateInterpolator.png

一個(gè)屬性 android:factor

AnticipateInterpolator

public class AnticipateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
    private final float mTension;

    public AnticipateInterpolator() {
        mTension = 2.0f;
    }

    public AnticipateInterpolator(float tension) {
        mTension = tension;
    }

    public AnticipateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

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

}
AnticipateInterpolator.png

有一個(gè)屬性 android:tension

AnticipateOvershootInterpolator

public class AnticipateOvershootInterpolator extends BaseInterpolator
        implements NativeInterpolatorFactory {
    private final float mTension;

    public AnticipateOvershootInterpolator() {
        mTension = 2.0f * 1.5f;
    }

    public AnticipateOvershootInterpolator(float tension) {
        mTension = tension * 1.5f;
    }

    public AnticipateOvershootInterpolator(float tension, float extraTension) {
        mTension = tension * extraTension;
    }

    public AnticipateOvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

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

}
AnticipateOvershootInterpolator.png

有兩個(gè)屬性 android:tensionandroid:extraTension

BounceInterpolator

public class BounceInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

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

}
BounceInterpolator.png

CycleInterpolator

public class CycleInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

    public CycleInterpolator(Resources resources, Theme theme, AttributeSet attrs) {
        TypedArray a;

        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;
}
CycleInterpolator.png

有一個(gè)屬性 android:cycles

DecelerateInterpolator

public class DecelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

    public DecelerateInterpolator(float factor) {
        mFactor = factor;
    }

    public DecelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

        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;

}
DecelerateInterpolator.png

有一個(gè)屬性 android:factor

LinearInterpolator

public class LinearInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

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

}
LinearInterpolator.png

OvershootInterpolator

public class OvershootInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
    private final float mTension;

    public OvershootInterpolator() {
        mTension = 2.0f;
    }

    public OvershootInterpolator(float tension) {
        mTension = tension;
    }

    public OvershootInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

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

}
OvershootInterpolator.png

有一個(gè)屬性 android:tension

PathInterpolator

public class PathInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {

    // This governs how accurate the approximation of the Path is.
    private static final float PRECISION = 0.002f;

    private float[] mX; // x coordinates in the line

    private float[] mY; // y coordinates in the line

    /**
     * 用 Path 構(gòu)建
     * Path 開(kāi)始前必須是 (0,0)冷溃,結(jié)束時(shí)必須是 (1,1)
     */
    public PathInterpolator(Path path) {
        initPath(path);
    }

    /**
     * 用 (x,y) 坐標(biāo)點(diǎn)構(gòu)建
     * 起點(diǎn)是 (0,0),結(jié)束是 (1,1)梦裂,參數(shù)是貝塞爾曲線的控制點(diǎn)坐標(biāo)
     */
    public PathInterpolator(float controlX, float controlY) {
        initQuad(controlX, controlY);
    }

    /**
     * 貝塞爾曲線的兩個(gè)控制點(diǎn)
     */
    public PathInterpolator(float controlX1, float controlY1, float controlX2, float controlY2) {
        initCubic(controlX1, controlY1, controlX2, controlY2);
    }

    public PathInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;

parseInterpolatorFromTypeArray(a);
        setChangingConfiguration(a.getChangingConfigurations());
        a.recycle();
    }

    private void parseInterpolatorFromTypeArray(TypedArray a) {
        // 如果 xml 定義了 pathData 屬性似枕,那么 Path 路徑就完全用這個(gè)
        if (a.hasValue(R.styleable.PathInterpolator_pathData)) {
            String pathData = a.getString(R.styleable.PathInterpolator_pathData);
            Path path = PathParser.createPathFromPathData(pathData);
            if (path == null) {
                throw new InflateException("The path is null, which is created"
                        + " from " + pathData);
            }
            initPath(path);
        } else {
            // 說(shuō)明沒(méi)有定義 pathData 時(shí)必須定義 controlX1 和 controlY1 這一對(duì)控制點(diǎn)以繪制貝塞爾曲線
            if (!a.hasValue(R.styleable.PathInterpolator_controlX1)) {
                throw new InflateException("pathInterpolator requires the controlX1 attribute");
            } else if (!a.hasValue(R.styleable.PathInterpolator_controlY1)) {
                throw new InflateException("pathInterpolator requires the controlY1 attribute");
            }
            float x1 = a.getFloat(R.styleable.PathInterpolator_controlX1, 0);
            float y1 = a.getFloat(R.styleable.PathInterpolator_controlY1, 0);

            boolean hasX2 = a.hasValue(R.styleable.PathInterpolator_controlX2);
            boolean hasY2 = a.hasValue(R.styleable.PathInterpolator_controlY2);
            // controlX2,controlY2 要么同時(shí)有年柠,要么同時(shí)沒(méi)有凿歼。多加一個(gè)控制點(diǎn)
            if (hasX2 != hasY2) {
                throw new InflateException(
                        "pathInterpolator requires both controlX2 and controlY2 for cubic Beziers.");
            }

            if (!hasX2) {
                initQuad(x1, y1);
            } else {
                float x2 = a.getFloat(R.styleable.PathInterpolator_controlX2, 0);
                float y2 = a.getFloat(R.styleable.PathInterpolator_controlY2, 0);
                initCubic(x1, y1, x2, y2);
            }
        }
    }

    private void initQuad(float controlX, float controlY) {
        Path path = new Path();
        path.moveTo(0, 0);
        path.quadTo(controlX, controlY, 1f, 1f);
        initPath(path);
    }

    private void initCubic(float x1, float y1, float x2, float y2) {
        Path path = new Path();
        path.moveTo(0, 0);
        path.cubicTo(x1, y1, x2, y2, 1f, 1f);
        initPath(path);
    }

    private void initPath(Path path) {
        float[] pointComponents = path.approximate(PRECISION);

        int numPoints = pointComponents.length / 3;
        if (pointComponents[1] != 0 || pointComponents[2] != 0
                || pointComponents[pointComponents.length - 2] != 1
                || pointComponents[pointComponents.length - 1] != 1) {
            throw new IllegalArgumentException("The Path must start at (0,0) and end at (1,1)");
        }

        mX = new float[numPoints];
        mY = new float[numPoints];
        float prevX = 0;
        float prevFraction = 0;
        int componentIndex = 0;
        for (int i = 0; i < numPoints; i++) {
            float fraction = pointComponents[componentIndex++];
            float x = pointComponents[componentIndex++];
            float y = pointComponents[componentIndex++];
            if (fraction == prevFraction && x != prevX) {
                throw new IllegalArgumentException(
                        "The Path cannot have discontinuity in the X axis.");
            }
            if (x < prevX) {
                throw new IllegalArgumentException("The Path cannot loop back on itself.");
            }
            mX[i] = x;
            mY[i] = y;
            prevX = x;
            prevFraction = fraction;
        }
    }

    /**
     * Path 繪制曲線確定的函數(shù) <code>y = f(x)</code>,速度就按這個(gè)變化
     */
    @Override
    public float getInterpolation(float t) {
        if (t <= 0) {
            return 0;
        } else if (t >= 1) {
            return 1;
        }
        // 二分查找
        int startIndex = 0;
        int endIndex = mX.length - 1;

        while (endIndex - startIndex > 1) {
            int midIndex = (startIndex + endIndex) / 2;
            if (t < mX[midIndex]) {
                endIndex = midIndex;
            } else {
                startIndex = midIndex;
            }
        }

        float xRange = mX[endIndex] - mX[startIndex];
        if (xRange == 0) {
            return mY[startIndex];
        }

        float tInRange = t - mX[startIndex];
        float fraction = tInRange / xRange;

        float startY = mY[startIndex];
        float endY = mY[endIndex];
        return startY + (fraction * (endY - startY));
    }
}

有五個(gè)屬性 android:pathData冗恨,android:controlX1答憔,android:controlY1android:controlX2掀抹,android:controlY2虐拓。

Support V4 下的兼容插值器

LookupTableInterpolator 是一個(gè)抽象類,子類要傳入一個(gè) float 數(shù)組傲武,根據(jù)傳入的 input 返回蓉驹,這個(gè)值就是用數(shù)組里已經(jīng)定義好的數(shù)字按一定的算法返回。

abstract class LookupTableInterpolator implements Interpolator {

    private final float[] mValues;
    private final float mStepSize;

    public LookupTableInterpolator(float[] values) {
        mValues = values;
        mStepSize = 1f / (mValues.length - 1);
    }

    @Override
    public float getInterpolation(float input) {
        if (input >= 1.0f) {
            return 1.0f;
        }
        if (input <= 0f) {
            return 0f;
        }

        int position = Math.min((int) (input * (mValues.length - 1)), mValues.length - 2);

        float quantized = position * mStepSize;
        float diff = input - quantized;
        float weight = diff / mStepSize;

        return mValues[position] + weight * (mValues[position + 1] - mValues[position]);
    }

}

三個(gè)繼承者揪利,區(qū)別在于 float 數(shù)組的值不同:

  • FastOutLinearInInterpolator
  • FastOutSlowInInterpolator
  • LinearOutSlowInInterpolator

自定義

res/anim 目錄下創(chuàng)建 my_overshoot_interpolator.xml态兴,修改原生插值器的屬性值:

<?xml version="1.0" encoding="utf-8"?>
<overshootInterpolator xmlns:android="http://schemas.android.com/apk/res/android"
    android:tension="7.0" />

然后使用自定義的插值器

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@anim/my_overshoot_interpolator"
    android:fromXScale="1.0"
    android:toXScale="3.0"
    android:fromYScale="1.0"
    android:toYScale="3.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="700" />
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市疟位,隨后出現(xiàn)的幾起案子瞻润,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绍撞,死亡現(xiàn)場(chǎng)離奇詭異正勒,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)楚午,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門昭齐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人矾柜,你說(shuō)我怎么就攤上這事阱驾。” “怎么了怪蔑?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵里覆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我缆瓣,道長(zhǎng)喧枷,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任弓坞,我火速辦了婚禮隧甚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘渡冻。我一直安慰自己戚扳,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布族吻。 她就那樣靜靜地躺著帽借,像睡著了一般。 火紅的嫁衣襯著肌膚如雪超歌。 梳的紋絲不亂的頭發(fā)上砍艾,一...
    開(kāi)封第一講書(shū)人閱讀 49,772評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音巍举,去河邊找鬼脆荷。 笑死,一個(gè)胖子當(dāng)著我的面吹牛懊悯,可吹牛的內(nèi)容都是我干的简烘。 我是一名探鬼主播,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼定枷,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了届氢?” 一聲冷哼從身側(cè)響起欠窒,我...
    開(kāi)封第一講書(shū)人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后岖妄,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體型将,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年荐虐,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了七兜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡福扬,死狀恐怖腕铸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情铛碑,我是刑警寧澤狠裹,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站汽烦,受9級(jí)特大地震影響涛菠,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜撇吞,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一俗冻、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧牍颈,春花似錦迄薄、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至人乓,卻和暖如春勤篮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背色罚。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工碰缔, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人戳护。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓金抡,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親腌且。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梗肝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容