您可以通过覆盖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().