2017-04-17 48 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();