先上代碼:
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分鍾后才能看到一個短暫停頓