0 から 360 の角度に達するまで、200 ミリ秒ごとに画像を回転させる ObjectAnimator があります。すべてが正常に機能します。所要時間は予想されます。1 サイクルで、アニメーションは 20 ~ 30 ミリ秒 (デバイスによって異なります) 多く実行されます。これは、200 ミリ秒ではなく 220 ミリ秒です。
要件は、画像を 360 度から 0 度に移動することです。10秒で。
画像の初期角度は に設定されており、画像の移動のみで角度に360
到達する必要があります。0
10 seconds
every 200 milliseconds
したがって、クリックするとボタンがあり、from と to の角度を使用してrotateメソッドを呼び出します
// initial angle is 360 and should reach 0 in 10 seconds with image moving only every 200 milliseconds.
rotate(360 , 360 - 7.2f); // from 360 to 352.8
// to run 10000 milliseconds with 200 milliseconds interval it would take 50 cycle for animation to run
// so the angle to move each cycle will be 360/50 = 7.2
Rotate メソッドは再帰的に呼び出され、次の角度に移動します。
private void rotate(float fromAngle , final float toAngle){
ImageView imageView = (ImageView) findViewById(R.id.rotate_image_view);
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "rotation", fromAngle, toAngle);
objectAnimator.setDuration(200);
objectAnimator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
Log.d(TAG , "End time " + System.currentTimeMillis());
if(toAngle > 0) {
float moveAngleTo = toAngle - 7.2f; // next angle position
// round next angle to 0 if goes to negative
if(moveAngleTo < 0){
moveAngleTo = 0;
}
rotate(toAngle , moveAngleTo);
}
}
@Override
public void onAnimationStart(Animator animation) {
Log.d(TAG , "Start time " + System.currentTimeMillis());
}
});
objectAnimator.start();
}
予想される合計持続時間は 10 秒ですが、各サイクルは 200 ミリ秒を超えて実行され、合計が 11 秒を超えます (つまり、20 x 50 = 1000 ミリ秒)。