2013-07-22 57 views
0

我使用這個XML代碼來旋轉imageVeiw 180度:簡單ImageView的旋轉

<ImageView 
    android:id="@+id/keyP2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btn4" 
    android:layout_centerHorizontal="true" 
    android:adjustViewBounds="true" 
    android:rotation="180" 
    android:maxWidth="125dp" 
    android:scaleType="fitCenter" 
    android:src="@drawable/image0_key" /> 

也能正常工作的Android API版本11及以上,但android:rotation="180"不支持低於API 11.我如何可以旋轉imageView 180度低於11?

回答

5

試試這一個。它爲我工作。

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/linear_interpolator" > 

    <rotate 
     android:duration="2000" 
     android:fromDegrees="0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toDegrees="360" > 
    </rotate> 

</set> 

將此文件保存在名爲「anim」的文件夾下的資源中。

RES /動畫/ rotate.xml

中的活動。添加此代碼。

Animation sampleFadeAnimation = AnimationUtils.loadAnimation(PreviewImageActivity.this,R.anim.rotate); 
    sampleFadeAnimation.setRepeatCount(1); 
    yourImageView.startAnimation(sampleFadeAnimation); 

做一些研究,你會得到很多屬性來改善這個旋轉動畫,如你所願。

感謝...

+0

我使用此代碼並將android:toDegrees =「360」更改爲android:toDegrees =「180」。動畫確實有效,但動畫完成後,imageview會立即切換回原始位置。如何在動畫播放後讓imageview保持其位置? – Shane

+1

好吧我解決了我的問題,在動畫之後添加了這段代碼:sampleFadeAnimation.setFillAfter(true);這將保持任何動畫完成後的狀態。謝謝您的幫助! – Shane

+0

繼續前進..感謝... – Srinivasan

0

您可能想嘗試通過擴展動畫以編程方式執行此操作。關閉我的頭頂像這樣:

RotateAnimation rotateAnim = new RotateAnimation(0f, 350f, 15f, 15f); 
rotateAnim.setInterpolator(new LinearInterpolator()); 
rotateAnim.setRepeatCount(Animation.INFINITE); 
rotateAnim.setDuration(700); 

// Start the animation 
final ImageView imgView = (ImageView) findViewById(R.id.YOUR_ID); 
imgView.startAnimation(rotateAnim); 

// Stop when necessary 
imgView.setAnimation(null); 

雖然我不完全確定RotateAnimation是否支持11之前。但你應該可以通過編程來完成。

編輯:Android Rotatable ImageView其他選項

+0

謝謝,但此代碼爲高於或低於API 11不旋轉圖像... – Shane

+0

對不起,沒有工作,這只是一個快速的嘗試。我添加了一個可能有用的鏈接。 – Phoenix

+0

感謝您的幫助,但也是對於api 11及以上。我可能只需要創建另一組在phothoshop中物理旋轉的圖像。 – Shane