单击按钮旋转图像视图

时间:2017-04-17 12:31:02

标签: java android

我想点击我的按钮来旋转我的图像视图。 我要这个: 当用户点击按钮时,将图像视图旋转90度。 再次单击我的按钮时,将图像视图旋转180度。 再次单击我的按钮时,将图像视图旋转270度。 再次单击我的按钮时,将图像视图旋转0度。 这就像这样继续...... 我可以通过以下代码正确旋转我的图像视图一次90度:

        mImageView.setRotation(0);

通过这个我可以旋转到90度并将其恢复到0度。

float deg = (mImageView.getRotation() == 90F) ? 0F : 90F;
    mImageView.animate().rotation(deg).setInterpolator(new AccelerateDecelerateInterpolator());

我怎么能这样做?

3 个答案:

答案 0 :(得分:3)

Button点击

上写下以下代码
imageView.setRotation(imageView.getRotation() + 90);

最初在完成整圈后,将0 rotation值设置为ImageView

答案 1 :(得分:0)

private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    imgview.startAnimation(rotateAnim);
}

答案 2 :(得分:0)

您可以创建旋转动画并将其设置为反向 重复动画,这种方式视图将旋转为所需的角度立即回到初始角度:

ImageView diskView = (ImageView) findViewById(R.id.imageView3);

// Create an animation instance
Animation an = new RotateAnimation(0.0f, 360.0f, pivotX, pivotY);

// Set the animation's parameters
an.setDuration(10000);               // duration in ms
an.setRepeatCount(1);                // -1 = infinite repeated
an.setRepeatMode(Animation.REVERSE); // reverses each repeat
an.setFillAfter(true);               // keep rotation after animation

// Aply animation to image view
diskView.setAnimation(an);
diskView.startAnimation();
相关问题