Android Object Animator的执行时间超过持续时间

时间:2016-03-03 02:41:05

标签: android animation rotateanimation

我有一个ObjectAnimator,每200毫秒旋转一次图像,直到从0到360度角度。一切正常,期待它所需的时间。对于一个周期,动画运行20到30毫秒(在设备上有所不同)更多,这是220毫秒而不是200毫秒。

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

图像初始角度设置为3600角度应该10 secondsevery 200 milliseconds只有 // 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

所以,点击时我有一个按钮,将调用旋转方法,其中from和to的角度为

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();
}

递归调用旋转方法,以移动到下一个角度。

userPlayString

预计总持续时间应为10秒,但每个周期运行超过200毫秒,这增加了超过11秒的总数(即20 x 50 = 1000毫秒)。

1 个答案:

答案 0 :(得分: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());
        }
    });