版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21138819/article/details/83994466
大家好,在上一篇中,我们介绍了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,大家可以安装之后自行查看
最后献上源码 github
参考资料:自定义控件三部曲之动画篇