如何在不旋转布局的情况下改变方向时旋转按钮?
How to rotate buttons when orientation changes without rotating the layout?
我想让 ImageButton 在设备方向改变时旋转。它应旋转 90、180、270 和 360 度,并且其相对布局应保持稳定,因此只有按钮会移动。如何才能做到这一点?我做了一些研究,但没有发现任何可以帮助我的东西。
您可以像这样通过覆盖 onConfigurationChanged()
来检测方向变化:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//call your rotate method here
}
然后一旦检测到方向更改事件,就可以像这样在 Button 上执行动画旋转:
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);
}
您可以在RotateAnimation
的构造函数中设置起始和结束角度,然后设置动画持续时间(以毫秒为单位)。然后,您只需在要设置动画的视图上调用 startAnimation()
。
我想让 ImageButton 在设备方向改变时旋转。它应旋转 90、180、270 和 360 度,并且其相对布局应保持稳定,因此只有按钮会移动。如何才能做到这一点?我做了一些研究,但没有发现任何可以帮助我的东西。
您可以像这样通过覆盖 onConfigurationChanged()
来检测方向变化:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//call your rotate method here
}
然后一旦检测到方向更改事件,就可以像这样在 Button 上执行动画旋转:
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);
}
您可以在RotateAnimation
的构造函数中设置起始和结束角度,然后设置动画持续时间(以毫秒为单位)。然后,您只需在要设置动画的视图上调用 startAnimation()
。