0

I am using this xml code to rotate an imageVeiw 180 degrees:

<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" />

This works fine for android api version 11 and up, but android:rotation="180" is not supported below api 11. How can I rotate an imageView 180 degrees for below 11?

Shane
  • 972
  • 2
  • 12
  • 27

2 Answers2

4

Try this one. it work for me.

<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>

Save this file in resources under a folder named "anim".

res/anim/rotate.xml

Within your activity. Add this code.

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

Do some research you will get lot of properties to improve this rotate animation as your wish.

thanks...

Hades
  • 3,916
  • 3
  • 34
  • 74
Srinivasan
  • 4,481
  • 3
  • 28
  • 36
  • I used this code and changed android:toDegrees="360" to android:toDegrees="180". The animation does work but after the animation is complete, the imageview instantly switches back to its original position. How can I make the imageview keep its position after the animation is played? – Shane Jul 23 '13 at 16:58
  • 1
    Ok I fixed my problem by adding this code after the animation: sampleFadeAnimation.setFillAfter(true); This will keep the state of the any animation after it is completed. Thanks for the help! – Shane Jul 23 '13 at 19:50
0

You may want to try doing it programmatically by extending Animation. Off the top of my head something like:

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

Although I am not totally sure if RotateAnimation is supported pre-11. But you should be able to do it programmatically.

edit: refer to Android Rotatable ImageView for additional options

Community
  • 1
  • 1
Phoenix
  • 1,881
  • 5
  • 20
  • 28
  • Sorry that didn't work, that's just a quick try. I added a link that may help. – Phoenix Jul 23 '13 at 00:10
  • Thanks for your help but that is also for api 11 and up. I might just have to create another set of images that are physically rotated in phothoshop. – Shane Jul 23 '13 at 00:16
  • 2
    Shoot, sorry man. Sorry those were no good. Maybe there's a way to find the bounds of the bitmap and just invert the greatest y axis by 180. – Phoenix Jul 23 '13 at 01:11