【Interpolator插補(bǔ)器】
在使用動畫的過程中,有一個interpolator插補(bǔ)器的屬性??梢允褂眠@個屬性來修飾動畫運(yùn)動的速率。比如加速、先加速再加速等。
安卓api提供幾個已經(jīng)實現(xiàn)好的插補(bǔ)器:
- LinearInterpolator 直線插補(bǔ)器(勻速)
- DecelerateInterpolator 減速插補(bǔ)器(先快后慢)
- AccelerateInterpolator 加速插補(bǔ)器(先慢后快)
- AccelerateDecelerateInterpolator 加速減速插補(bǔ)器(先慢后快再慢)
- AnticipateInterpolator 向前插補(bǔ)器(先往回跑一點,再加速向前跑)
- OvershootInterpolator 超出插補(bǔ)器(向前跑直到越界一點后,再往回跑)
- AnticipateOvershootInterpolator 向前向后插補(bǔ)器(先往回跑一點,再向后跑一點,再回到終點)
- BounceInterpolator 反彈插補(bǔ)器(在動畫結(jié)束的時候回彈幾下,如果是豎直向下運(yùn)動的話,就是玻璃球下掉彈幾下的效果)
- CycleInterpolator 循環(huán)插補(bǔ)器(按指定的路徑以指定時間(或者是偏移量)的1/4、變速地執(zhí)行一遍,再按指定的軌跡的相反反向走1/2的時間,再按指定的路徑方向走完剩余的1/4的時間,最后回到原點。假如:默認(rèn)是讓a從原點往東跑100米。它會先往東跑100米,然后往西跑200米,再往東跑100米回到原點。可在代碼中指定循環(huán)的次數(shù))
使用
方式1:
anim.setInterpolator(new AnticipateOvershootInterpolator());
方式2:
資源文件
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:interpolator="@android:anim/overshoot_interpolator"
android:fromXDelta="0"
android:toXDelta="200%"
android:duration="1000"/>
</set>
如果以上這些插值器都無法滿足我們的具體開發(fā)需求的話,那么也可以通過繼承TimeInterpolator類來自定義自己的Interpolator,在自定義的Interpolator類中只需要要重寫getInterpolation(float input)方法,并在該方法通過相關(guān)的計算,再返回計算后的結(jié)果(插值因子)就搞定,具體示例如下:
public class CustomInterpolator implements TimeInterpolator {
@Override
public float getInterpolation(float input) {
// 編寫相關(guān)的邏輯計算
//input *= 0.8f;
return input * input;
}
}
【TypeEvaluator估值器】
使用TypeEvaluator根據(jù)插值因子計算屬性值,Android系統(tǒng)可識別的類型包括int、float和顏色,分別由 IntEvaluator、 FloatEvaluator、 ArgbEvaluator 提供支持,如下:

同樣的,如何系統(tǒng)提供的TypeEvaluator不能滿足我們的要求,我們也可以通過繼承TypeEvaluator類來定義自己的Evaluator,在自定義的TypeEvaluator類中只需要要重寫getInterpolation(float input)方法,并在該方法通過相關(guān)的計算,再返回計算后的結(jié)果(插值因子)就搞定,具體示例如下:
import android.animation.TypeEvaluator;
import android.annotation.SuppressLint;
@SuppressLint("NewApi")
public class CustomEvaluator implements TypeEvaluator<Number> {
@Override
public Float evaluate(float fraction, Number startValue, Number endValue) {
// TODO Auto-generated method stub
float propertyResult = 0;
/*float startFloat = startValue.floatValue();
return (startFloat + fraction * (endValue.floatValue() - startFloat));*/
return propertyResult;
}
}
使用 ArgbEvaluator
tv有一個函數(shù):public void setBackgroundColor(int color); 可以改變背景色
ValueAnimator 中也曾改變過背景色,使用的是 ArgbEvaluator。在這里我們再回顧下 ArgbEvaluator,它的實現(xiàn)代碼如下:
public class ArgbEvaluator implements TypeEvaluator {
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24);
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = (Integer) endValue;
int endA = (endInt >> 24);
int endR = (endInt >> 16) & 0xff;
int endG = (endInt >> 8) & 0xff;
int endB = endInt & 0xff;
return (int)((startA + (int)(fraction * (endA - startA))) << 24) |
(int)((startR + (int)(fraction * (endR - startR))) << 16) |
(int)((startG + (int)(fraction * (endG - startG))) << 8) |
(int)((startB + (int)(fraction * (endB - startB))));
}
}
使用:ArgbEvaluator 的返回值是 Integer 類型,所以我們要使用 ArgbEvaluator 的話,構(gòu)造 ObjectAnimator 時必須使用 ofInt()
ObjectAnimator animator = ObjectAnimator.ofInt(tv, "BackgroundColor", 0xffff00ff, 0xffffff00, 0xffff00ff);
animator.setDuration(8000);
animator.setEvaluator(new ArgbEvaluator());
animator.start();