《Android自定义控件入门到精通》文章索引 ☞ https://blog.csdn.net/Jhone_csdn/article/details/118146683
《Android自定义控件入门到精通》所有源码 ☞ https://gitee.com/zengjiangwen/Code
文章目录
Interpolation插值器
在补间动画的介绍中,我们使用了Interpolator,Interpolator是用于控制动画如何运动的作用。
物体的运动我们知道有匀速运动和变速运动,匀速运动比较简单,物体从A到B运动速度是一样的,变速运动就比较复杂了,有先快后慢,先慢后快等各种情况,
在动画运用中,我们就是通过interpolator来控制(描述)动画的运动过程。
我们用t来表示当前动画运动的时间,t∈[01],用y来表示当前运动距离的百分比,y∈[01]
举例:
匀速运动,y=t
整个运动过程我们用y、t坐标系画出来应该就是一条直线
反过来,给出一条y、t曲线/直线,就能知道这个物体从起点到终点的运动过程,线上点的斜率反应当前的速度,斜率越大,当前速度越大,Interpolator就是用来定义这条直线的算法的
Anroid中的Interpolator实现了TimeInterpolator接口
public interface TimeInterpolator {
float getInterpolation(float input);
}
getInterpolation() 描述的就是运动的公式(算法),自定义插值器只要在这里描述运动就可以了
- input为运动开始到结束的时间值,用0表示开始的时间,1表示结束的时间,即我们上面说的时间t
- return的值为物体所在的距离,用0表示开始的位置,1表示终点的位置,即我们上面说的y,在运动过程中,物体可能会运动到超过终点的位置后返回终点,也可能先往起点相反的位置运动后再朝终点的位置运动,也就是y∈[0,1],但y在运动过程中,可以小于0,也可以大于1
Android已提供的Interpolation:
LinearInterpolator
//LinearInterpolator.java
@Override
public float getInterpolation(float input) {
return input;
}
算法 :y=t
结论:LinearInterpolator为匀速运动
AccelerateInterpolator
public AccelerateInterpolator() {
mFactor = 1.0f;
mDoubleFactor = 2.0;
}
public AccelerateInterpolator(float factor) {
mFactor = factor;
mDoubleFactor = 2 * mFactor;
}
factor:可以理解为运动算法效果的影响因子
@Override
public float getInterpolation(float input) {
if (mFactor == 1.0f) {
return input * input;
} else {
return (float)Math.pow(input, mDoubleFactor);
}
}
算法 y=t^2f (f=factor)
结论:
- 当factory>0.5f(factory=0.5f,为直线匀速运动)时,物体做加速运动
- 当factory<0.5f时,物体先加速后减速运动
AccelerateDecelerateInterpolator
@Override
public float getInterpolation(float input) {
return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) +