Animation插值器:解决Android Animation 循环执行的停顿问题

730 阅读2分钟

「这是我参与2022首次更文挑战的第30天,活动详情查看:2022首次更文挑战

前言

在Android开发中,有时候我们需要一个动画一直循环执行下去,常见的如laoding菊花一直旋转,这时候就需要使用Animation的repeat功能,示例代码如下:

animation = new RotateAnimation(0f, 360f, lightView.getWidth() / 2, lightView.getHeight() / 2);
animation.setDuration(2500);
animation.setRepeatMode(Animation.RESTART);
animation.setRepeatCount(Animation.INFINITE);
lightView.startAnimation(animation);

为lightView执行以中心为圆点的旋转动画。看看相关的两个参数:

  • repeatMode设为RESTART,在动画执行结束后继续从头执行。由于单次动画是旋转一周,这样两次动画可以衔接在一起。

  • repeatCount设为INFINITE来保证无限循环执行动画。

在实际的展示效果中,你会发现每次动画播放完都会停顿一下再去播放下次动画,动画无法连贯的执行。

其实并不是动画间有停顿,而是animation的默认插值器的效果。

解决

解决方法很简单,为Animation设置线性插值器即可,代码如下:

animation.setInterpolator(new LinearInterpolator());

这样我们解决了问题,但是为什么要这么做呢?来看看Animation的构造函数源码:

public Animation() {
    ensureInterpolator();
}
 
public Animation(Context context, AttributeSet attrs) {
    TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.Animation);
    ...
 
    ensureInterpolator();
}

可以看到构造函数中都会调用ensureInterpolator函数,这个函数源码如下:

protected void ensureInterpolator() {
    if (mInterpolator == null) {
        mInterpolator = new AccelerateDecelerateInterpolator();
    }
}

所以,Animation默认会使用AccelerateDecelerateInterpolator这个插值器,这个插值器的作用是动画播放有个加速减速的过程,类似汽车启动和刹车的效果,开始播放时会从静止状态逐渐加速直到匀速,接近结束时会逐渐减速直至停止。所以当我们未设置其他插值器的时候,由于这个插值器的作用导致动画效果上无法连贯,有一个停顿的效果。

而线性插值器LinearInterpolator的效果是动画全程匀速执行,这样当我们设置了这个插值器动画就可以连贯起来。

插值器极大的丰富了Animation的效果,我们可以通过系统自带的或者自定义的插值器来完成很多酷炫的效果,大家有兴趣可以找资料看一下。