一、描述:
我们在设计动画时,通常都会用到一些Interpolator,而它的作用就是控制动画的速度,即可以理解为:
Interpolator是一个速度控制器,控制速度变化。
Interpolator借口只有一个抽象方法getInterpolation(float input),而系统也自带了几个Interpolater供我们使用:
1. AccelerateInterpolator: 动画从开始到结束,变化率是一个加速的过程。
2. DecelerateInterpolator: 动画从开始到结束,变化率是一个减速的过程。
3. CycleInterpolator: 动画从开始到结束,变化率是循环给定次数的正弦曲线。
4. AccelerateDecelerateInterpolator: 动画从开始到结束,变化率是先加速后减速的过程。
5. LinearInterpolator: 动画从开始到结束,变化率是线性变化。
除了以上几种,我们也可以继承Interpolater来定义自己的插补器:
import android.view.animation.Interpolator; public class MyInterpolator implements Interpolator { private float mFactor; private int iFactor; public MyInterpolator(int factor) { this.iFactor = factor; } @Override public float getInterpolation(float input) { switch (iFactor) { case 1: mFactor = input; // 性线匀速 break; case 2: mFactor = input * input * input; // 加速 break; } return mFactor; } }
二、Interpolater — 策略模式:
2.1 接口类:TimeInterpolator
package android.animation; /** * A time interpolator defines the rate of change of an animation. This allows animations * to have non-linear motion, such as acceleration and deceleration. */ public interface TimeInterpolator { /** * Maps a value representing the elapsed fraction of an animation to a value that represents * the interpolated fraction. This interpolated value is then multiplied by the change in * value of an animation to derive the animated value at the current elapsed animation time. * * @param input A value between 0 and 1.0 indicating our current point * in the animation where 0 represents the start and 1.0 represents * the end * @return The interpolation value. This value can be more than 1.0 for * interpolators which overshoot their targets, or less than 0 for * interpolators that undershoot their targets. */ float getInterpolation(float input); }
讲的很清楚:TimeInterpolator定义了一个可以改变动画速率的时间插补器,它允许你的动画可以是非线性行为:如加速和减速。
2.2 Interpolator:
package android.view.animation; import android.animation.TimeInterpolator; /** * An interpolator defines the rate of change of an animation. This allows * the basic animation effects (alpha, scale, translate, rotate) to be * accelerated, decelerated, repeated, etc. */ public interface Interpolator extends TimeInterpolator { // A new interface, TimeInterpolator, was introduced for the new android.animation // package. This older Interpolator interface extends TimeInterpolator so that users of // the new Animator-based animations can use either the old Interpolator implementations or // new classes that implement TimeInterpolator directly. }
我擦!想必大家都无语,只是告诉我们Interpolater扩展了TimeInterpolator,其它什么都没。
2.3 插补器
开头已经介绍了几种系统自带的,也举了个自定义的类,这些都没有任何关系,只不过提供给开发者,我有几种策略给你用,而你只需要在Animation中,set一下就行,其它的就交给Animation帮你去完成你想要的动画效果。
三、总结:
getInterpolation(float input)中的input是从哪里来的?
它是由Animation类在getTransformation中会调用getInterpolater,input = (当前时间-开始时间)/总时长;
即在TimeInterpolator中定义input = [0.0f, 1.0f]区间。
关于Animation,我会抽空讲解它。