2013-07-22 58 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