Android RotateAnimation,如何执行更多旋转?

发布于 2024-12-03 03:54:12 字数 1598 浏览 5 评论 0原文

我必须在单击按钮时旋转 ImageView 。第一次单击时,它必须向右旋转,第二次单击时必须向左旋转,等等。

问题是,当我尝试第二次旋转“刚刚旋转”的图像时,旋转从原点开始,而不是从“第一次旋转后”开始“ 观点。

我需要旋转先前旋转产生的图像。下面我把代码过去了。

public class Rotate extends Activity {
    boolean mDirRight = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotate);
        final ImageView imageArray = (ImageView) findViewById(R.id.ImageViewArray);
        imageArray.setImageResource(R.drawable.array01);
        imageArray.setAdjustViewBounds(true);
        final Button btnRotate = (Button) findViewById (R.id.ButtonRotate);
        btnRotate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doRotation();
            }
        });    
    }

    private void doRotation(){
        final int rotationRight = 30;
        final int rotationLeft = -20;
        final RotateAnimation rAnim;
        int degree;
        if (mDirRight) {
            degree = rotationRight;
            mDirRight = false;
        } else {
            degree = rotationLeft;
            mDirRight = true;
        }
        final ImageView image = (ImageView) findViewById(R.id.ImageViewArray);
        rAnim = new RotateAnimation(0f, degree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        rAnim.setStartOffset(0);
        rAnim.setDuration(2000);
        rAnim.setFillAfter(true);
        rAnim.setFillEnabled(true);
        image.startAnimation(rAnim);
    }
}

I have to rotate an ImageView on a Button click. At the first click it have to rotate to right, at the second to left etc.

The problem is that when I try to rotate for second time the "just rotated" image, the rotation start from original point and not from "post first rotation" point.

I need to rotate the image resulting from previous rotation. Below I past the code.

public class Rotate extends Activity {
    boolean mDirRight = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rotate);
        final ImageView imageArray = (ImageView) findViewById(R.id.ImageViewArray);
        imageArray.setImageResource(R.drawable.array01);
        imageArray.setAdjustViewBounds(true);
        final Button btnRotate = (Button) findViewById (R.id.ButtonRotate);
        btnRotate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                doRotation();
            }
        });    
    }

    private void doRotation(){
        final int rotationRight = 30;
        final int rotationLeft = -20;
        final RotateAnimation rAnim;
        int degree;
        if (mDirRight) {
            degree = rotationRight;
            mDirRight = false;
        } else {
            degree = rotationLeft;
            mDirRight = true;
        }
        final ImageView image = (ImageView) findViewById(R.id.ImageViewArray);
        rAnim = new RotateAnimation(0f, degree, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        rAnim.setStartOffset(0);
        rAnim.setDuration(2000);
        rAnim.setFillAfter(true);
        rAnim.setFillEnabled(true);
        image.startAnimation(rAnim);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-12-10 03:54:12

在这一行

rAnim = new RotateAnimation(0f, 度数,
旋转动画.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);

将 0f 更改为所需的起始角度。

In this line

rAnim = new RotateAnimation(0f, degree,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);

change 0f to the desired starting angle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文