Android动画(三) Interpolator插值器

本文深入探讨了Android中动画插值器的使用,包括多种预设插值器如加速减速插值器、线性插值器等的工作原理,以及如何自定义插值器来实现更复杂的动画效果。

一.常用插值器

  • AccelerateDecelerateInterpolator   在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
  • AccelerateInterpolator                     在动画开始的地方速率改变比较慢,然后开始加速
  • AnticipateInterpolator                      开始的时候向后然后向前甩
  • AnticipateOvershootInterpolator     开始的时候向后然后向前甩一定值后返回最后的值
  • BounceInterpolator                          动画结束的时候弹起
  • CycleInterpolator                             动画循环播放特定的次数,速率改变沿着正弦曲线
  • DecelerateInterpolator                    在动画开始的地方快然后慢
  • LinearInterpolator                            以常量速率改变
  • OvershootInterpolator                      向前甩一定值后再回到原来位置

二.自定义插植器

先看LinearInterpolator的实现

public class LinearInterpolator implements Interpolator {
 
    public LinearInterpolator() {
    }
 
    public LinearInterpolator(Context context, AttributeSet attrs) {
    }
 
    public float getInterpolation(float input) {
        return input;
    }
}
public interface Interpolator extends TimeInterpolator {
}

LinearInterpolator实现了Interpolator接口;而Interpolator接口则直接继承自TimeInterpolator,而且并没有添加任何其它的方法。 
那我们来看看TimeInterpolator接口都有哪些函数吧:

/**
 * 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);我们来讲讲这个函数是干什么的。 
参数input:input参数是一个float类型,它取值范围是0到1,表示当前动画的进度,取0时表示动画刚开始,取1时表示动画结束,取0.5时表示动画中间的位置,其它类推。 
返回值:表示当前实际想要显示的进度。取值可以超过1也可以小于0,超过1表示已经超过目标值,小于0表示小于开始位置。 

所以只要重写getInterpolation就可以自定义插值器

public class MyEvaluator implements TypeEvaluator<Integer> {
    @Override
    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
        int startInt = startValue;
        return (int)(200+startInt + fraction * (endValue - startInt));
    }
}

详细细节请看

自定义控件三部曲之动画篇(二)——Interpolator插值

转载博客地址: https://blog.youkuaiyun.com/harvic880925/article/details/40049763

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值