2016-01-20 69 views
-2

我有一個imageview圖層,我想要將它旋轉一下並停下來,當我再次觸摸時,我該怎麼辦?Android - 如何在圖像上旋轉圖像查看

+0

同樣的問題被回答在這裏 - http://stackoverflow.com/questions/8981845/android-rotate-image-in-imageview-by-an-angle –

回答

1

動畫 DIR

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 
    <rotate 
     android:duration="2500" 
     android:interpolator="@android:anim/linear_interpolator" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:repeatCount="infinite" 
     android:repeatMode="restart" 
     android:toDegrees="360" /> 
</set> 

現在,在你的類文件加載動畫

Animation animation = AnimationUtils.loadAnimation(getContext(),R.anim.rotate); 

而且對你的形象點擊開始動畫創建動畫rotate.xml

your_image.startAnimation(animation); 
+0

感謝和最後一個問題如果我再次觸摸需要停止動畫的圖像,我該如何處理? – kingmaker

+0

採取一個布爾isAnimationRunning,並在動畫開始時將其設置爲true,反之亦然。如果沒有運行,則只需單擊圖像即可開始動畫。 –

0

您可以嘗試以編程方式設置動畫這樣的:

RotateAnimation rotate = new RotateAnimation(0, 360, 
    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 
    0.5f); 

    rotate.setDuration(4000); 
    rotate.setRepeatCount(Animation.INFINITE); 
    yourView.setAnimation(rotate); 

希望它能幫助。

+0

如何做ontouch旋轉和ontouch再次停止旋轉? – kingmaker