最近開發android camera相關的程式,被螢幕旋轉搞得頭大,一方面得考慮螢幕旋轉後布局的變化,另一方面得搞清楚螢幕的旋轉方向、角度與camera的preview角度的關係。本來通過過載activity的onconfigurationchanged方法,可以檢測到螢幕旋轉,但發現有乙個問題,它只能檢測水平方向與垂直方向的切換,無法檢測180度的跳轉(例如:水平方向突然轉180度到水平方向),所以最後不得不換成orientationeventlistener方法來解決問題。在這裡分享下經驗,並就此順便總結下android開發中螢幕旋轉的處理吧。
1. 不做任何處理的情況下
如果想很好地支援螢幕旋轉,則建議在res中建立layout-land和layout-port兩個資料夾,把橫屏和豎屏的布局檔案放入對應的layout資料夾中。
2. 如何設定固定的螢幕方向
在androidmanifest.xml對應的 activity 屬性中,新增:
1
2
android:screenorientation=
"landscape"
//橫屏
android:screenorientation=
"portrait"
//豎屏
那麼,預設的情況下,應用啟動後,會固定為指定的螢幕方向,即使螢幕旋轉,activity也不會出現銷毀或者轉向等任何反應。
3. 強制開啟螢幕旋轉效果
如果使用者的手機沒有開啟重力感應器或者在androidmanifest.xml中設定了android:screenorientation,預設情況下,該activity不會響應螢幕旋轉事件。如果在這種情況下,依然希望activity能響應螢幕旋轉,則新增如下**:
1
2
// activity的 oncreate 函式中
this
.setrequestedorientation(activityinfo.screen_orientation_full_sensor);
4. 螢幕旋轉時,不希望activity被銷毀
如果希望捕獲螢幕旋轉事件,並且不希望activity 被銷毀,方法如下:
(1)在androidmanifest.xml對應的activity屬性中,新增:
1
android:configchanges=
"orientation|screensize"
(2)在對應的activity中,過載函式onconfigurationchanged
1
2
3
4
@override
public
voidonconfigurationchanged(configuration newconfig)
在該函式中可以通過兩種方法檢測當前的螢幕狀態:
第一種:
判斷newconfig是否等於configuration.orientation_landscape,configuration.orientation_portrait
當然,這種方法只能判斷螢幕是否為橫屏,或者豎屏,不能獲取具體的旋轉角度。
第二種:
呼叫this.getwindowmanager().getdefaultdisplay().getrotation();
該函式的返回值,有如下四種:
su***ce.rotation_0,su***ce.rotation_90,su***ce.rotation_180,su***ce.rotation_270
其中,su***ce.rotation_0 表示的是手機豎屏方向向上,後面幾個以此為基準依次以順時針90度遞增。
(3) 這種方法的bug
最近發現這種方法有乙個bug,它只能一次旋轉90度,如果你突然一下子旋轉180度,onconfigurationchanged函式不會被呼叫。
5. 實時判斷螢幕旋轉的每乙個角度
上面說的各種螢幕旋轉角度的判斷至多只能判斷 0,90,180,270 四個角度,如果希望實時獲取每乙個角度的變化,則可以通過orientationeventlistener 來實現。
使用方法:
(1)建立乙個類繼承orientationeventlistener 1
2
3
4
5
6
7
8
9
public
class
myorientationdetector
extends
orientationeventlistener
@override
public
void
onorientationchanged(
int
orientation)
}
(2)開啟和關閉監聽
可以在 activity 中建立myorientationdetector 類的物件,注意,監聽的開啟的關閉,是由該類的父類的 enable() 和 disable() 方法實現的。
因此,可以在activity的 onresume() 中呼叫myorientationdetector 物件的 enable方法,在 onpause() 中呼叫myorientationdetector 物件的 disable方法來完車功能。
(3)監測指定的螢幕旋轉角度
myorientationdetector類的onorientationchanged 引數orientation是乙個從0~359的變數,如果只希望處理四個方向,加乙個判斷即可:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if
(orientation == orientationeventlistener.orientation_unknown)
//只檢測是否有四個角度的改變
if
( orientation >
350
|| orientation<
10
)
else
if
( orientation >
80
&&orientation <
100
)
else
if
( orientation >
170
&&orientation <
190
)
else
if
( orientation >
260
&&orientation <
280
)
else
log.i(
"myorientationdetector "
,
"onorientationchanged:"
+orientation);
本文出自 「jhuster的專欄」 部落格,請務必保留此出處
Android螢幕旋轉
如果只想設定螢幕橫屏或者豎屏,只需要設定橫豎屏 android screenorientation landscape android screenorientation portrait 這樣設定後即使螢幕旋轉,activity也不會出現銷毀或方向旋轉等反應,螢幕只有乙個方向。需要動態改變橫豎屏設...
Android禁止旋轉螢幕
禁止螢幕隨手機旋轉變化 有時候我們希望讓乙個程式的介面始終保持在乙個方向,不隨手機方向旋轉而變化 在androidmanifest.xml的每乙個需要禁止轉向的activity配置中加入android screenorientation landscape 屬性。landscape 橫向 portr...
iOS開發 螢幕旋轉
設定 當前檢視控制器 支援旋轉的方向 bool shouldautorotate 設定當前檢視控制器 支援旋轉的方向 nsuinteger supportedinte ceorientations 設定 螢幕旋轉並且控制view上檢視在橫屏和豎屏狀態居中 重寫layoutsubviews方法 voi...