編輯:關於 Android 程式設計
想真正吃透 Android 動畫,光會調 API 是不夠的,得潛到源碼裡去看它是怎麼算的。本文延續前面的入門內容,從實現層面拆解動畫背後的兩位主角——插值器(Interpolator)與估值器(Evaluator)。
時間插值器本質上就是一個映射關係 y = f(t),這裡的 t 並不是真實秒數,而是 elapsed time / duration 得出的歸一化比例,落在 0 到 1 之間。系統自帶的每一個插值器都繼承自 BaseInterpolator,內部結構高度相似。下面用 AccelerateInterpolator 當樣本:
public class AccelerateInterpolator extends BaseInterpolator implements NativeInterpolatorFactory {
private final float mFactor;
private final double mDoubleFactor;
// 無參構造,factor 預設為 1
public AccelerateInterpolator() {
mFactor = 1.0f;
mDoubleFactor = 2.0;
}
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);
}
// 資源裡沒寫 factor 時,預設 1.0f
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);
}
}
三層繼承關係其實很單薄:BaseInterpolator 實現了 Interpolator,而 Interpolator 自己什麼都沒定義,只是乾脆地繼承了 TimeInterpolator。
abstract public class BaseInterpolator implements Interpolator {
private int mChangingConfiguration;
/** @hide */
public int getChangingConfiguration() {
return mChangingConfiguration;
}
/** @hide */
void setChangingConfiguration(int changingConfiguration) {
mChangingConfiguration = changingConfiguration;
}
}
真正訂下規則的是 TimeInterpolator,全體插值器的靈魂方法就在這裡:
public interface TimeInterpolator {
/*
* @param input 動畫已執行的時間比例,∈[0,1]
* @return 插值換算後的值
*/
float getInterpolation(float input);
}
public LinearInterpolator(),無任何可調參數y = t
()(factor 預設 1)、(float factor)、(Context, AttributeSet)y = t^(2f),f 為加速因子,預設 1;缺省即 y = t^2
y = 1 - (1 - t)^(2f);缺省為 y = 2t - t^2
public AccelerateDecelerateInterpolator(),沒有可調參數y = 0.5cos((t+1)π) + 0.5
()(tension 預設 2)、(float tension)、(Context, AttributeSet)y = t*t*((s+1)t - s);缺省為 y = t*t*(3t-2)
()(tension 預設 2)、(float tension)、(Context, AttributeSet)y = (t-1)(t-1)((s+1)(t-1) + s) + 1;缺省為 y = (t-1)(t-1)(3t-1) + 1
()(tension 預設 3)、(float tension)(內部乘以 1.5)、(float tension, float extraTension)、(Context, AttributeSet)y = 2t*t*(2t*s + 2t - s),缺省 y = 2t*t*(8t-3)y = 2(t-1)(t-1)(2(s+1)(t-1) + s) + 1,缺省 y = 2(t-1)(t-1)(8t-5) + 1
public BounceInterpolator(),無參數y = 8*(1.1226t)^2y = 8*(1.1226t - 0.54719)^2 + 0.7y = 8*(1.1226t - 0.8526)^2 + 0.9y = 8*(1.1226t - 1.0435)^2 + 0.95
(float cycles)、(Context, AttributeSet)(cycles 預設 1)y = sin(2*c*t*π);缺省為 y = sin(2*t*π)
插值器算完的只是一個 0 到 1 之間的比例,把它變成具體的透明度、座標或顏色,是估值器的活儿。所有估值器都實現了帶泛型的 TypeEvaluator,自訂時只要照著實作一個方法就好:
public interface TypeEvaluator<T> {
/*
* @param fraction 插值器換算後的值
* @param startValue 屬性起始值
* @param endValue 屬性結束值
* @return 換算後的值
*/
public T evaluate(float fraction, T startValue, T endValue);
}
處理 Integer 型屬性,結果與 fraction 呈一次函數關係。
public class IntEvaluator implements TypeEvaluator<Integer> {
// result = x0 + t * (x1 - x0)
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int) (startInt + fraction * (endValue - startInt));
}
}
public class FloatEvaluator implements TypeEvaluator<Number> {
public Float evaluate(float fraction, Number startValue, Number endValue) {
float startFloat = startValue.floatValue();
return startFloat + fraction * (endValue.floatValue() - startFloat);
}
}
顏色動畫的關鍵:把 ARGB 四個通道拆開,各自線性插值後再拼回去。
public class ArgbEvaluator implements TypeEvaluator {
private static final ArgbEvaluator sInstance = new ArgbEvaluator();
/** @hide 註:構造方法並未宣告為 private,因此這個單例對上層開發者其實不可見 */
public static ArgbEvaluator getInstance() {
return sInstance;
}
public Object evaluate(float fraction, Object startValue, Object endValue) {
int startInt = (Integer) startValue;
int startA = (startInt >> 24) & 0xff;
int startR = (startInt >> 16) & 0xff;
int startG = (startInt >> 8) & 0xff;
int startB = startInt & 0xff;
int endInt = (Integer) endValue;
int endA = (endInt >> 24) & 0xff;
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)));
}
}
插值器決定「動得快慢」,估值器決定「值是多少」,兩者串起來才是完整的屬性動畫。 nine 種內建插值器的差異,本質就是九條不同的 y = f(t) 曲線;三個內建估值器的實作則相當樸素,幾乎都是線性內插。
最容易踩的坑只有一個:傳進 evaluate() 的 fraction 已經是插值器加工過的比例,而不是原始的 elapsed time。搞錯這一點,自訂插值器就會完全失效。
用 SlidingMenu 實作側滑選單並切換 Fragment
側滑抽屜是另一種常見的主選單形式。本文示範以開源的 SlidingMenu 庫實作左側滑出選單,點擊項目後切換右側的 Fragment 內容。 效果如下: 主
QQ厘米秀「挑戰厘米神搶手」怎麼玩?入口、規則與獎品說明
厘米秀是手機QQ把PC年代的QQ秀搬到手機上的嘗試:一個會動、會換裝、還會在對話框裡做動作的虛擬分身。除了聊天時多一個角色站在訊息旁邊,官方也不定期推出小遊戲,其中「挑戰
Android AIDL 入門:跨進程通訊的語法與實作
AIDL 是什麼 AIDL 全名是 Android Interface Definition Language,是一種用來描述跨進程通訊介面的語言。當不同 App 或不同
Android 進階:Service 的兩種啟動方式與生命週期解析
前面介紹過 Activity 與 Intent,這次來談 Service。如果把 Activity 比喻成前台程式,那麼 Service 就是後台程式——它的整個生命週期