# 1. 插值器Interpolator
前面提到過,在`xml`中使用`android:interpolator`用來指定一個插值器,它會影響動畫的播放速度。系統常見的插值器有:
* 線性插值器,`LinearInterpolator`。
* 加速減速插值器:`AccelerateDecelerateInterpolator`。
* 減速插值器:`DecelerateInterpolator`。
這里以`LinearInterpolator`為案例:
~~~java
public class LinearInterpolator extends BaseInterpolator implements NativeInterpolator {
public LinearInterpolator() {
}
public LinearInterpolator(Context context, AttributeSet attrs) {
}
public float getInterpolation(float input) {
return input;
}
/** @hide */
@Override
public long createNativeInterpolator() {
return NativeInterpolatorFactory.createLinearInterpolator();
}
}
~~~
從代碼可以知道輸入和輸出都是`input`,也就是說斜率為常數。這里舉一個案例,在`40ms`內,使用線性插值器將`View`的`x`從`0`變換到`40`。動畫的刷新頻率為`10ms/幀`,那么我們可以得出下面幾個刷新時刻:
| 時間段 | 0 | 1 |2|3|4|
| --- | --- |--- |--- |--- |--- |
| t | 0ms | 10ms | 20ms | 30ms| 40ms |
具體的`x`變為了什么值,這里就需要使用估值器,比如:
~~~
public class IntEvaluator implements TypeEvaluator<Integer> {
/**
* This function returns the result of linearly interpolating the start and end values, with
* <code>fraction</code> representing the proportion between the start and end values. The
* calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>,
* where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>,
* and <code>t</code> is <code>fraction</code>.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue The start value; should be of type <code>int</code> or
* <code>Integer</code>
* @param endValue The end value; should be of type <code>int</code> or <code>Integer</code>
* @return A linear interpolation between the start and end values, given the
* <code>fraction</code> parameter.
*/
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(startInt + fraction * (endValue - startInt));
}
}
~~~
也就是傳入當前的百分比,得到估值。計算公式為:
```
result = x0 + t * (x1 - x0)
```
簡單理解一下也就是起始位置`x0`加上傳入的比例`t`根據其移動距離轉化后的長度值。
那么,類似的這里簡單摘要一下`AccelerateDecelerateInterpolator`的源碼:
~~~
public class AccelerateDecelerateInterpolator extends BaseInterpolator
implements NativeInterpolator {
public AccelerateDecelerateInterpolator() {
}
@SuppressWarnings({"UnusedDeclaration"})
public AccelerateDecelerateInterpolator(Context context, AttributeSet attrs) {
}
public float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
}
}
~~~
其計算也就是:

當然,我們也可以定義自己的插值器。
# 2. 估值器Evaluator
前面我們使用過`ArgbEvaluator`,主要用來計算改變后的百分比對應的值。比如:`IntEvaluator`、`FloatEvaluator`和`ArgbEvaluator`。
上面介紹了`IntEvaluator`,我們知道其計算也就是根據比例來計算值。那么對應的`FloatEvaluator`這里也簡單看下:
~~~
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);
}
}
~~~
可以看到這里的計算和`IntEvaluator`保持一致,只是類型不同。
同樣的,也可以自定義一個估值器來計算。
- 介紹
- UI
- MaterialButton
- MaterialButtonToggleGroup
- 字體相關設置
- Material Design
- Toolbar
- 下拉刷新
- 可折疊式標題欄
- 懸浮按鈕
- 滑動菜單DrawerLayout
- NavigationView
- 可交互提示
- CoordinatorLayout
- 卡片式布局
- 搜索框SearchView
- 自定義View
- 簡單封裝單選
- RecyclerView
- xml設置點擊樣式
- adb
- 連接真機
- 小技巧
- 通過字符串ID獲取資源
- 自定義View組件
- 使用系統控件重新組合
- 旋轉菜單
- 輪播圖
- 下拉輸入框
- 自定義VIew
- 圖片組合的開關按鈕
- 自定義ViewPager
- 聯系人快速索引案例
- 使用ListView定義側滑菜單
- 下拉粘黏效果
- 滑動沖突
- 滑動沖突之非同向沖突
- onMeasure
- 繪制字體
- 設置畫筆Paint
- 貝賽爾曲線
- Invalidate和PostInvalidate
- super.onTouchEvent(event)?
- setShadowLayer與陰影效果
- Shader
- ImageView的scaleType屬性
- 漸變
- LinearGradient
- 圖像混合模式
- PorterDuffXfermode
- 橡皮擦效果
- Matrix
- 離屏繪制
- Canvas和圖層
- Canvas簡介
- Canvas中常用操作總結
- Shape
- 圓角屬性
- Android常見動畫
- Android動畫簡介
- View動畫
- 自定義View動畫
- View動畫的特殊使用場景
- LayoutAnimation
- Activity的切換轉場效果
- 屬性動畫
- 幀動畫
- 屬性動畫監聽
- 插值器和估值器
- 工具
- dp和px的轉換
- 獲取屏幕寬高
- JNI
- javah命令
- C和Java相互調用
- WebView
- Android Studio快捷鍵
- Bitmap和Drawable圖像
- Bitmap簡要介紹
- 圖片縮放和裁剪效果
- 創建指定顏色的Bitmap圖像
- Gradle本地倉庫
- Gradle小技巧
- RxJava+Okhttp+Retrofit構建網絡模塊
- 服務器相關配置
- node環境配置
- 3D特效