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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With