1

I have an image button that I want to rotate when the orientation of the device changes. How can I rotate the image with some animation or transition ?

Meeth
  • 81
  • 7
  • [check this discussion](http://stackoverflow.com/questions/19753912/set-image-orientation-using-exifinterface) – vITs May 15 '14 at 12:15
  • [RotateAnimation](http://developer.android.com/reference/android/view/animation/RotateAnimation.html) ? – Blackbelt May 15 '14 at 12:17
  • @vits that post refers to changing orientation of an image from gallery. My question is regarding an ImageButton that should adjust as the device changes orientation – Meeth May 15 '14 at 12:23
  • then you can check for RotateAnimation – vITs May 15 '14 at 12:28

1 Answers1

3

try this code snippet.

rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate

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

</set>

rorate_anticlockwise.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="0"
android:duration="2000" />
</set>

for checking orientation of the phone use this code

int orientation =this.getResources().getConfiguration().orientation;

the complete code for MainActivity.java is

public class MainActivity  extends Activity{

/* (non-Javadoc)
 * @see android.app.Activity#onCreate(android.os.Bundle)
 */
ImageButton image_btn;

Animation ranim_clockwise, ranim_anticlockwise;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image_btn= (ImageButton)findViewById(R.id.imageButton1);
    ranim_clockwise = AnimationUtils.loadAnimation(this,R.anim.rotate);
    ranim_anticlockwise = AnimationUtils.loadAnimation(this,R.anim.rotate_anticlock);
    int orientation =this.getResources().getConfiguration().orientation;
    if(orientation==1){ // portrait mode
        image_btn.setAnimation(ranim_clockwise);
    }
    if(orientation==2){  //landscape mode
        image_btn.setAnimation(ranim_anticlockwise);
    }

}

}

hopefully it will help you.

Irshad Khan
  • 794
  • 6
  • 21
  • After rotation the image button goes back to it's original position I need the image button to remain in the new position – Meeth May 15 '14 at 15:53
  • @Meeth i have edited my code having added anticlockwise rotation xml file and set it in different mode to rotate imagebutton. – Irshad Khan May 15 '14 at 18:32
  • I have used an Image Button for a Custom Camera app. My objective was to rotate the Image Button as the User rotates the device specific to potrait, landscape, reverse_potrait & reverse_landscape modes both in clockwise & anticlockwise direction. – Meeth May 16 '14 at 12:47
  • @Meeth change the rotate.xml file according to your need and check orientation of phone and apply specific rotation on imagebutton. – Irshad Khan May 17 '14 at 05:09