Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate buttons when orientation changes without rotating the layout?

I want to make an ImageButton rotate when the device orientation changes. It should rotate for 90, 180, 270 and 360 angles and its relative layout should remain steady so only the buttons move. How can this be done? I've done some research but found nothing that could help me.

like image 745
Lekstadt Avatar asked Oct 06 '15 21:10

Lekstadt


1 Answers

You can detect an orientation change by Overriding onConfigurationChanged() like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    //call your rotate method here 
}

Then once you detect the orientation change event, you can perform an animated rotation on a Button like this:

public void rotateView(View view) {
    Button button = (Button) view.findViewById(R.id.some_button);
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360); 
    rotateAnimation.setDuration(2000);
    button.startAnimation(rotateAnimation);
}

You can set the starting and ending angle in the constructor of RotateAnimation, then set the duration for how long the animation should take (in milliseconds). Then you simply call startAnimation() on the view you want to animate.

like image 142
NoChinDeluxe Avatar answered Nov 15 '22 21:11

NoChinDeluxe