動畫——插值器(interpolator)

java類 xml資源id 說明
AccelerateDecelerateInterpolator @android:anim/accelerate_decelerate_interpolator 其變化開始和結(jié)束速率較慢歌粥,中間加速
AccelerateInterpolator @android:anim/accelerate_interpolator 其變化開始速率較慢绘证,后面加速
DecelerateInterpolator @android:anim/decelerate_interpolator 其變化開始速率較快栽连,后面減速
LinearInterpolator @android:anim/linear_interpolator 其變化速率恒定
AnticipateInterpolator @android:anim/anticipate_interpolator 其變化開始向后甩龄砰,然后向前
AnticipateOvershootInterpolator @android:anim/anticipate_overshoot_interpolator 其變化開始向后甩厂僧,然后向前甩蚁袭,過沖到目標值黎炉,最后又回到了終值
OvershootInterpolator @android:anim/overshoot_interpolator 其變化開始向前甩玷室,過沖到目標值桃煎,最后又回到了終值
BounceInterpolator @android:anim/bounce_interpolator 其變化在結(jié)束時反彈
CycleInterpolator @android:anim/cycle_interpolator 循環(huán)播放篮幢,其速率為正弦曲線
TimeInterpolator 一個接口,可以自定義插值器
pathInterpolator path插值器为迈,一般要自定義xml傳入path

使用:

<objectAnimator
    android:propertyName="translateY"
    android:valueType="floatType"
    android:valueFrom="-100"
    android:valueTo="600"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:duration="1500"
    android:interpolator="@android:anim/accelerate_interpolator">
</objectAnimator>

自定義

以accelerate_interpolator為例
在源碼中三椿,對應(yīng)的文件是:

frameworks\base\core\res\res\anim\linear_accelerate.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
/* //device/apps/common/res/anim/linear_interpolator.xml
**
** Copyright 2007, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->

<accelerateInterpolator/>
frameworks\base\core\java\android\view\animation\AnimationUtils.java
private static Interpolator createInterpolatorFromXml(Resources res, Theme theme, XmlPullParser parser)
        throws XmlPullParserException, IOException {

    BaseInterpolator interpolator = null;

    // Make sure we are on a start tag.
    int type;
    int depth = parser.getDepth();

    while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
            && type != XmlPullParser.END_DOCUMENT) {

        if (type != XmlPullParser.START_TAG) {
            continue;
        }

        AttributeSet attrs = Xml.asAttributeSet(parser);

        String name = parser.getName();

        if (name.equals("linearInterpolator")) {
            interpolator = new LinearInterpolator();
        } else if (name.equals("accelerateInterpolator")) {
            interpolator = new AccelerateInterpolator(res, theme, attrs);
        } else if (name.equals("decelerateInterpolator")) {
            interpolator = new DecelerateInterpolator(res, theme, attrs);
        } else if (name.equals("accelerateDecelerateInterpolator")) {
            interpolator = new AccelerateDecelerateInterpolator();
        } else if (name.equals("cycleInterpolator")) {
            interpolator = new CycleInterpolator(res, theme, attrs);
        } else if (name.equals("anticipateInterpolator")) {
            interpolator = new AnticipateInterpolator(res, theme, attrs);
        } else if (name.equals("overshootInterpolator")) {
            interpolator = new OvershootInterpolator(res, theme, attrs);
        } else if (name.equals("anticipateOvershootInterpolator")) {
            interpolator = new AnticipateOvershootInterpolator(res, theme, attrs);
        } else if (name.equals("bounceInterpolator")) {
            interpolator = new BounceInterpolator();
        } else if (name.equals("pathInterpolator")) {
            interpolator = new PathInterpolator(res, theme, attrs);
        } else {
            throw new RuntimeException("Unknown interpolator name: " + parser.getName());
        }
    }
    return interpolator;
}

可見,最后調(diào)用的是new AccelerateInterpolator ()

frameworks\base\core\java\android\view\animation\AccelerateInterpolator .java
/*
 * Copyright (C) 2006 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view.animation;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources.Theme;
import android.content.res.TypedArray;
import android.util.AttributeSet;

import com.android.internal.R;
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 rate of change starts out slowly and
 * and then accelerates.
 *
 */
@HasNativeInterpolator
public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
    private final float mFactor;
    private final double mDoubleFactor;

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

    /**
     * Constructor
     *
     * @param factor Degree to which the animation should be eased. Seting
     *        factor to 1.0f produces a y=x^2 parabola. Increasing factor above
     *        1.0f  exaggerates the ease-in effect (i.e., it starts even
     *        slower and ends evens faster)
     */
    public AccelerateInterpolator(float factor) {
        mFactor = factor;
        mDoubleFactor = 2 * mFactor;
    }

    public AccelerateInterpolator(Context context, AttributeSet attrs) {
        this(context.getResources(), context.getTheme(), attrs);
    }

    /** @hide */
    public AccelerateInterpolator(Resources res, Theme theme, AttributeSet attrs) {
        TypedArray a;
        if (theme != null) {
            a = theme.obtainStyledAttributes(attrs, R.styleable.AccelerateInterpolator, 0, 0);
        } else {
            a = res.obtainAttributes(attrs, R.styleable.AccelerateInterpolator);
        }

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

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

通過重寫getInterpolation(float input)來控制動畫播放

自定義

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末葫辐,一起剝皮案震驚了整個濱河市搜锰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌耿战,老刑警劉巖蛋叼,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異昆箕,居然都是意外死亡鸦列,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門鹏倘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來薯嗤,“玉大人,你說我怎么就攤上這事纤泵÷娼悖” “怎么了镜粤?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長玻褪。 經(jīng)常有香客問我肉渴,道長,這世上最難降的妖魔是什么带射? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任同规,我火速辦了婚禮,結(jié)果婚禮上窟社,老公的妹妹穿的比我還像新娘券勺。我一直安慰自己,他們只是感情好灿里,可當(dāng)我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布关炼。 她就那樣靜靜地躺著,像睡著了一般匣吊。 火紅的嫁衣襯著肌膚如雪儒拂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天色鸳,我揣著相機與錄音社痛,去河邊找鬼。 笑死缕碎,一個胖子當(dāng)著我的面吹牛褥影,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播咏雌,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼校焦!你這毒婦竟也來了赊抖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤寨典,失蹤者是張志新(化名)和其女友劉穎氛雪,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體耸成,經(jīng)...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡报亩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了井氢。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片弦追。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖花竞,靈堂內(nèi)的尸體忽然破棺而出劲件,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布零远,位于F島的核電站苗分,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏牵辣。R本人自食惡果不足惜摔癣,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望纬向。 院中可真熱鬧择浊,春花似錦、人聲如沸罢猪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽膳帕。三九已至粘捎,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間危彩,已是汗流浹背攒磨。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留汤徽,地道東北人娩缰。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像谒府,于是被迫代替她去往敵國和親拼坎。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,037評論 2 355

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