简体   繁体   English

如何在不改变布局的情况下更改方向时旋转按钮?

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

I want to make an ImageButton rotate when the device orientation changes. 我想在设备方向改变时使ImageButton旋转。 It should rotate for 90, 180, 270 and 360 angles and its relative layout should remain steady so only the buttons move. 它应旋转90度,180度,270度和360度角度,其相对布局应保持稳定,以便只有按钮移动。 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: 您可以通过覆盖onConfigurationChanged()来检测方向更改,如下所示:

@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: 然后,一旦检测到方向更改事件,就可以在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);
}

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). 您可以在RotateAnimation的构造函数中设置起始角度和结束角度,然后设置动画应该花费多长时间的持续时间(以毫秒为单位)。 Then you simply call startAnimation() on the view you want to animate. 然后,您只需在要设置动画的视图上调用startAnimation()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM