多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 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; } } ~~~ 其计算也就是: ![](https://img.kancloud.cn/80/23/8023b8602443109fa379470942f0fe3f_265x105.png) 当然,我们也可以定义自己的插值器。 # 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`保持一致,只是类型不同。 同样的,也可以自定义一个估值器来计算。