天天看點

Android控制螢幕方向的改變

Android控制螢幕方向的改變

  • 版 主位址:http://ghostfromheaven.iteye.com/blog/967629
  • 僅為備忘

     目前大多數手機都支援 重力感應 ,随之而來的就是螢幕方向改變的問題。很多遊戲都是僅橫屏展示的,也有一些是僅豎屏展示的,更多的是橫屏豎屏都可以的。

    對應普通開發者來說,螢幕的随意改變也會帶來困擾。在Google自帶的doc裡可以看到

  如果裝置的配置(在 Resources.Configuration 中進行了定義)發生改變,那麼所有使用者界面上的東西都需要進行更新,以适應新的配置。因為 Activity 是與使用者互動的最主要的機制,它包含了處理配置改變的專門支援。除非你特殊指定,否則當配置發生改變(比如螢幕方向、語言、輸入裝置等等的改變)時你目前的 activity 都将被銷毀,這銷毀是通過一個正常的activity 生命周期過程( onFreeze(Bundle) , onPause() , onStop() , 和 onDestroy() )進行的。如果 activity 之前正在前景畫面,當這個執行個體的 onDestroy() 調用完成後将會啟動這個 activity 的一個新的執行個體,并将前面那個執行個體中 onFreeze(Bundle) 所儲存的内容傳遞給新的執行個體。因為任何的應用資源(包括 layout 檔案)都有可能由于任何配置值而改變。是以處理配置改變的唯一安全的方法就是重新擷取所有的資源,包括 layout 、繪圖資源(原文 drawables )、字元串資源。由于 activity 已經如何儲存自己的狀态并從這些狀态中重建自身,是以 activity 重新啟動自身來獲得新的配置将是一個非常便利的途徑。在一些特殊的情況中,你可能希望當一種或者多種配置改變時避免重新啟動你的 activity 。你可以通過在manifest中設定android:configChanges 屬性來實作這點。你可以在這裡聲明 activity 可以處理的任何配置改變,當這些配置改變時不會重新啟動 activity ,而會調用 activity 的 onConfigurationChanged(Resources.Configuration) 方法。如果改變的配置中包含了你所無法處理的配置(在 android:configChanges 并未聲明),你的 activity 仍然要被重新啟動,而onConfigurationChanged(Resources.Configuration) 将不會被調用。

   在螢幕方向改變的時候,如果沒有處理,程式會自動重新開機。對應一些需要儲存使用者資料的應用中,必須處理這種情況。

1>在AndroidManifest.xml中設定Activity的android:configChanges 屬性。如:

Java代碼  

Android控制螢幕方向的改變
  1. <activity android:name=".AndroidLight"  
  2.                   android:label="@string/app_name"  
  3.                   android:configChanges="orientation|keyboardHidden">  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.                 <category android:name="android.intent.category.LAUNCHER" />  
  7.             </intent-filter>  
  8.         </activity>  

這樣就指定了螢幕方向改變和鍵盤隐藏時通知程式。

2>在程式中可以添加處理事件

Java代碼  

Android控制螢幕方向的改變
  1. @Override  
  2.     public void onConfigurationChanged(Configuration newConfig) {  
  3.       super.onConfigurationChanged(newConfig);  
  4.       Log.d(TAG," == onConfigurationChanged");  
  5.       processLayout();//自定義函數處理配置改變事件  
  6.     }  

3>也可以在AndroidManifest.xml中設定Activity的android:screenOrientation屬性。如

Xml代碼  

Android控制螢幕方向的改變
  1. <activity android:name=".AndroidLight"  
  2.                   android:label="@string/app_name"  
  3.                   android:screenOrientation="portrait">  
  4.             <intent-filter>  
  5.                 <action android:name="android.intent.action.MAIN" />  
  6.                 <category android:name="android.intent.category.LAUNCHER" />  
  7.             </intent-filter>  
  8. </activity>  

這樣指定螢幕方向為豎屏。螢幕就不會自動旋轉了。

4> android:configChanges 可選屬性值 

Constant Value Description
mcc 0x0001 The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.
mnc 0x0002 The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.
locale 0x0004 The locale has changed, that is the user has selected a new language that text should be displayed in.
touchscreen 0x0008 The touchscreen has changed. Should never normally happen.
keyboard 0x0010 The keyboard type has changed, for example the user has plugged in an external keyboard.
keyboardHidden 0x0020 The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.
navigation 0x0040 The navigation type has changed. Should never normally happen.
orientation 0x0080 The screen orientation has changed, that is the user has rotated the device.
screenLayout 0x0100 The screen layout has changed. This might be caused by a different display being activated.
uiMode 0x0200 The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.
fontScale 0x40000000 The font scaling factor has changed, that is the user has selected a new global font size.

5>android:screenOrientation的可選屬性值

Constant Value Description
unspecified -1 No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device
landscape Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data.
portrait 1 Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data.
user 2 Use the user's current preferred orientation of the handset.
behind 3 Keep the screen in the same orientation as whatever is behind this activity.
sensor 4 Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device.
nosensor 5 Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device.
sensorLandscape 6 Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing.
sensorPortait 7 Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing.
reverseLandscape 8 Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape.
reversePortait 9 Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait.
fullSensor 10 Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation).

繼續閱讀