【Android 动画】动画详解之插值器(二)

本文深入探讨Android动画中的插值器Interpolator,介绍其作用及9种预设插值器特性,包括加速、减速、弹跳等效果,通过实例演示如何在旋转动画中应用不同插值器。

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

大家好,在上一篇中,我们介绍了Android 的补间动画,这一篇我们来说说动画的另外一个公共属性插值器Interpolator

在上一节中,实现的旋转、位移动画等动画,我们会发现它一直是匀速的,但如果我们需要做一个加速的旋转的动画时,该如何做?

这就是Interpolator的由来,安卓系统已经为我们添加了9种插值器,让我们一起来了解下

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

接着,我们实现一个旋转动画

        rotateAnimation = new RotateAnimation(0, 3600, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setDuration(5000);

设置插值器

        switch (view.getId()) {
            case R.id.btn_1:
                //AccelerateDecelerateInterpolator    在动画开始与结束比较慢,中间加速
                rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_2:
                //AccelerateInterpolator                      加速
                rotateAnimation.setInterpolator(new AccelerateInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_3:
                //AnticipateInterpolator                      开始的时候向后然后向前甩
                rotateAnimation.setInterpolator(new AnticipateInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_4:
                //AnticipateOvershootInterpolator     开始的时候向后然后向前甩一定值后返回最后的值
                rotateAnimation.setInterpolator(new AnticipateOvershootInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_5:
                //BounceInterpolator                          动画结束的时候弹起
                rotateAnimation.setInterpolator(new BounceInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_6:
                //CycleInterpolator                             动画循环播放特定的次数,速率改变沿着正弦曲线
                rotateAnimation.setInterpolator(new CycleInterpolator(0.5f));
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_7:
                //DecelerateInterpolator                      减速
                rotateAnimation.setInterpolator(new DecelerateInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_8:
                //LinearInterpolator                            以常量速率改变
                rotateAnimation.setInterpolator(new LinearInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
            case R.id.btn_9:
                //OvershootInterpolator                      向前甩一定值后再回到原来位置
                rotateAnimation.setInterpolator(new OvershootInterpolator());
                tvDemo.startAnimation(rotateAnimation);
                break;
        }

TIP: 由于动画有点快,gif录制的文件比较大而且效果不好,所以这里贴上apk,大家可以安装之后自行查看

image.png
image.png

最后献上源码 github

参考资料:自定义控件三部曲之动画篇

你的认可,是我坚持更新博客的动力,如果觉得有用,就请点个赞,谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值