android 如何使用 RotateAnimation 将图像旋转两次

android how to rotate an image twice using RotateAnimation

我正在使用旋转动画将图像旋转到一定程度。在同一屏幕上,我希望用户通过单击 button.but 将同一图像从上一个位置旋转到下一个位置。旋转没有任何反应。

///inside oncreate 

RotateAnimation rotate = new RotateAnimation(0, rotateSpeed,
                        Animation.RELATIVE_TO_SELF, 0.9f,
                        Animation.RELATIVE_TO_SELF, 0.5f);

                rotate.setDuration(4000);
                rotate.setFillAfter(true);
                imv.setAnimation(rotate);

///inside button click

RotateAnimation rotate = new RotateAnimation(start, start+end,
                Animation.RELATIVE_TO_SELF, 0.9f,
                Animation.RELATIVE_TO_SELF, 0.5f);

        rotate.setDuration(2000);
        rotate.setFillAfter(true);
        imv.setAnimation(rotate);

将您的视图和旋转值传递给此方法。希望这能解决您的问题。

public void rotateView(final View v, final int rotation) {

    v.animate().setDuration(250).rotation(rotation).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationStart(Animator animation) {
            super.onAnimationStart(animation);
        }

        @Override
        public void onAnimationCancel(Animator animation) {
            super.onAnimationCancel(animation);
        }

        @Override
        public void onAnimationEnd(Animator animation) {
            super.onAnimationEnd(animation);
        }
    });
}
RotateAnimation rotate = new RotateAnimation(0, 360,
        Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
        0.5f);

rotate.setDuration(4000);
rotate.setRepeatCount(-1); // here you can set repeatcoount
yourView.setAnimation(rotate)

希望对您有所帮助。

干杯!

哦,在应用新的旋转动画之前,我只是清除了已经应用的旋转动画。

view.clearAnimation();