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

I want the ImageButton to rotate when the device orientation changes. It should rotate 90, 180, 270 and 360 angles, and its relative layout should remain stable, so only the buttons move. How can this be done? I did some research, but did not find anything that could help me.

+5
source share
1 answer

You can define a change in orientation by overriding onConfigurationChanged() as follows:

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

Then, as soon as you detect an orientation change event, you can perform an animated rotation on the button as follows:

 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 start and end angles in the RotateAnimation constructor, and then set the duration of the animation duration (in milliseconds). Then you just call startAnimation() on the view you want to animate.

+2
source

Source: https://habr.com/ru/post/1233096/


All Articles