0

我有一个 ObjectAnimator,每 200 毫秒旋转一次图像,直到它从 0 到 360 度角。一切正常,预计需要的持续时间。对于一个周期,动画会多运行 20 到 30 毫秒(因设备而异),即 220 毫秒而不是 200 毫秒。

要求是将图像从 360 度移动到 0 度角。在 10 秒内。

图像初始角度设置为仅图像移动360时应达到0的角度。10 secondsevery 200 milliseconds

因此,单击时我有一个按钮,将使用 from 和 to 角度调用旋转方法

      // 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 毫秒)。

4

1 回答 1

0

试试下面的代码:

ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mView, "rotation", fromAngle, toAngle);

    objectAnimator.setDuration(200);
    objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);
    objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
    objectAnimator.setInterpolator(new AccelerateInterpolator());

    objectAnimator.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            LogUtil.d(TAG , "End time " + System.currentTimeMillis());
        }

        @Override
        public void onAnimationStart(Animator animation) {
            LogUtil.d(TAG , "Start time " + System.currentTimeMillis());
        }
        @Override
        public void onAnimationRepeat(Animator animation) {
            animation.setInterpolator(new LinearInterpolator());
        }
    });
于 2016-03-03T03:02:38.190 回答