欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android动画之Interpolator

程序员文章站 2022-05-04 07:51:16
...

Android源码中,有两个Interpolator。一个是frameworks/base/graphics/java/android/graphics/Interpolator.java。另一个是
frameworks/base/core/java/android/view/animation/Interpolator.java。今天想要聊的是后者。

frameworks/base/core/java/android/view/animation/Interpolator.java的源码如下:

/**
 * 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.
}

Android在3.0引入属性动画时,加入了TimeInterpolator接口。Interpolator变成与之(TimeInterpolator)对应的旧接口。Interpolator继承TimeInterpolator,可以兼容旧代码。建议开发者在开发新代码的时候,直接实现TimeInterpolator接口。所以,我们需要再看一下TimeInterpolator的源码。

frameworks/base/core/java/android/animation/TimeInterpolator.java的源码如下:

/**
 * 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定义了动画变化的接口。float getInterpolation(float input)的输入是[0, 1.0],是动画插值器的归一化时间,0表示开始,1.0表示结束。返回值是动画插值器的归一化结果[0, 1.0],但是,返回值在超越目标(overshoot their targets)的时候,也可以<0或者>1.0。