[英]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()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.