Android动画之Interpolator

本文深入解析Android源码中Interpolator与TimeInterpolator接口的作用及区别。详细介绍了Interpolator如何定义动画速率变化,兼容新旧动画系统,以及TimeInterpolator如何实现非线性动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值