Android Easing Interpolator——一些动画插值器

Android动画系统中提供插值器Interpolator来改变动画的播放速率,实现不同的动态效果。Android系统提供了一些插值器的实现,比如LinearInterpolator、AccelerateInterpolator等。

Easing Function是一些插值函数的实现,在这里可以看到http://easings.net/zh-cn,在这些函数的计算里参数包含了起始值和时间,我们知道Interpolator的作用是将当前的线性时间比值转换为非线性值或其他,来改变动画的播放速度,

/**
 * 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);
}

转换之后交给TypeEvaluator计算得到

/**
 * Interface for use with the {@link ValueAnimator#setEvaluator(TypeEvaluator)} function. Evaluators
 * allow developers to create animations on arbitrary property types, by allowing them to supply
 * custom evaluators for types that are not automatically understood and used by the animation
 * system.
 *
 * @see ValueAnimator#setEvaluator(TypeEvaluator)
 */
public interface TypeEvaluator<T> {

    /**
     * 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.
     * @param endValue   The end value.
     * @return A linear interpolation between the start and end values, given the
     *         <code>fraction</code> parameter.
     */
    public T evaluate(float fraction, T startValue, T endValue);

}

TypeEvaluator里的计算参数里也有起始值,还有一个时间的比值,所以理论上是可以将Easing Function转换为Interpolator。

比如下面这个二次函数

	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	}
其中

t: current time, b: begInnIng value, c: change In value, d: duration
对应到上面evaluate的参数就可以得到

fraction=t/d; c=endValues-startValues; b=startValue。


下面是实现的一个demo

技术分享

代码在这里EaseAnimationInterpolator

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。