单击后图像从起始位置开始旋转

Image starts rotating from starting position after one click

我有一张旋转了 36° 的图像。 我使用了 setFillAfter() 和 setFillEnabled()。它正在保存位置,但如果我再次旋转它,它会从原始位置开始。我想让它从旋转后的位置开始。

Animation animation = new Animation (0,36, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(3000);
animation.setInterpolator(new LinearInterpolator());
animation.setFillAfter(true);

myImage.startAnimation(animation);

你只需要在Animation构造函数中传递imageview的当前角度,而不是每次都传递0。试试下面的代码:

        int rotationAngle=36;/*angle by which you want to rotate image*/
        int mCurrRotation = 0;  /*current angle of image*/

        buttonRotate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            float fromRotation = mCurrRotation;
            float toRotation = mCurrRotation += rotationAngle;

            Animation animation = new RotateAnimation(
                    fromRotation,
                    toRotation,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);


            animation.setDuration(3000);
            animation.setFillAfter(true);
            myImage.startAnimation(animation);
        }
    });

为此,在执行旋转后将图像的 x y 坐标设置到旋转后的新位置 ...