+ 我要发布
我发布的 我的标签 发现
浏览器扩展
斑点象@Edge

Android开发如何通过 RotateAnimation 设置控件的无限旋转

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); 该代码运行正常,但是有个问题,就是图片旋转一周后,会有一个非常小间隙的停顿。 停顿的原因是每个周期运行完后,会重新旋转,这个间隙造成了揉眼可见的停顿。 如何解决这个问题? 看一下 RotateAnimation 第二个参数 360f,这个参数表示从 0 度旋转到 360 度,即一个周期的角度,运行时间是 3000 秒。 运行周期是通过 animation.setDuration 来设置的。 如果是一个周期结束后重新开始会造成的短暂停顿,那将一个周期的旋转角度增加呢? 修改代码如下: 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); 运行代码,50分钟后才能看到一个停顿,因为设置的的旋转1000转为一个周期,每个周期耗时 3000 秒。 通过设置更长的旋转周期,就可以解决停顿的问题,毕竟谁会盯着屏幕看50分钟呢。
我的笔记