2017-04-17 47 views
-1

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

 mImageView.setRotation(0); 

,并通过这个我可以旋转90度,并返回到0度。

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

我该怎么办?

+0

检查此链接也可能会得到答案http://stackoverflow.com/a/18016331/4069985 [当点击 –

+0

可能的复制一个按钮在android中顺时针旋转图像](http://stackoverflow.com/questions/18016126/when-click-a-button-rotate-image-clockwise-in-android) –

回答

2

写下下面的代码对你Button点击

imageView.setRotation(imageView.getRotation() + 90); 

开始和完成整圈旋转后0 rotation值设置为ImageView

+0

是的,这是正确的答案...谢谢you – nsr

+0

@nsr很高兴帮助你... –

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