android RotateAnimation无限旋转

先上代码:

RotateAnimation animation =new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(3000); #3000毫秒旋转一周
animation.setRepeatCount(Animation.INFINITE); #无限循环
animation.setInterpolator(new LinearInterpolator()); #匀速圆周运动
animation.setFillAfter(true);
getView(R.id.img).setAnimation(animation);

运行后,能正常运行,但是发现,当图片循环一周后,会又一个非常小间隙的停顿。原因在于,每个周期运行完后,会重新旋转,这个间隙造成了停顿。

如何解决这个问题?答案就在代码标红部分。

360f 表示从0度旋转到360度,即一个周期的角度,运行时间是3000毫秒。如果是一个周期结束后重新开始造成的短暂停顿,那么,将一个周期的旋转角度增加呢?

重写代码:

int multiple=1000;//增加1000倍
float endDegrees=360f * multiple;
long duration=3000 * multiple;
RotateAnimation animation =new RotateAnimation(0f, endDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(duration);
animation.setRepeatCount(Animation.INFINITE); //无限循环
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);
getView(R.id.test).setAnimation(animation);

此时的一个旋转周期是3000秒,也就是50分钟后才能看到一个短暂停顿

posted @ 2020-10-18 13:26  乱炖er  阅读(1253)  评论(0编辑  收藏  举报