Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android 正確處理螢幕旋轉的適應問題

編輯:進階開發

現在的 Android 手機大多內建重力感應器,能偵測手機的翻轉,進而實現螢幕自動旋轉、重力感應遊戲等功能。要啟用螢幕自動旋轉其實很簡單,但伴隨而來的副作用更需要妥善處理。

啟用自動旋轉

只要在 AndroidManifest.xml 對應的 Activity 加上 screenOrientation="sensor" 屬性即可:

<activity
    android:name=".demo"
    android:label="@string/app_name"
    android:screenOrientation="sensor" >
</activity>

副作用:onCreate 被重複呼叫

螢幕自動旋轉帶來一個困擾:當視窗或佈局切換時,Activity 的 onCreate() 會被重複觸發。由於初始化資料通常寫在 onCreate 裡,重複執行可能釀成意料之外的後果。

解法:configChanges 與 onConfigurationChanged

在 Activity 加入 configChanges 屬性,並在 Java 中重載 onConfigurationChanged(),即可避免旋轉時重新走一次 onCreate:

<activity
    android:name="demo"
    android:configChanges="orientation|keyboardHidden"
    android:label="@string/app_name" >
</activity>
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 橫向處理
    } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        // 縱向處理
    }
}

進階:自訂版面配置

此外還有介面設計層面的考量。多數 Android 手機採用 HVGA、WVGA 解析度,螢幕視覺上偏「狹長」,直向看似合適的版面,橫向翻轉後往往顯示得別扭。這時可以把控制項由垂直線性版面改為水平線性版面。

做法是自訂一個繼承自 LinearLayout 的版面類別,覆寫 onMeasure()。當螢幕寬高改變時系統會呼叫此方法,我們便能取得新的寬高並據此調整:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int screenWith = View.MeasureSpec.getSize(widthMeasureSpec);
    int screenHeight = View.MeasureSpec.getSize(heightMeasureSpec);

    if (screenWith < screenHeight) {          // 直向版面
        this.setOrientation(VERTICAL);
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView instanceof CakyCanvas) {
                LayoutParams params = new LayoutParams(screenWith, screenHeight * 2 / 5);
                updateViewLayout(childView, params);
            } else if (childView instanceof CakyExplainCanvas) {
                LayoutParams params = new LayoutParams(screenWith, screenHeight * 3 / 5);
                updateViewLayout(childView, params);
            }
        }
    } else {                                  // 橫向版面
        this.setOrientation(HORIZONTAL);
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView instanceof CakyCanvas) {
                LayoutParams params = new LayoutParams(screenWith * 2 / 5, screenHeight);
                updateViewLayout(childView, params);
            } else if (childView instanceof CakyExplainCanvas) {
                LayoutParams params = new LayoutParams(screenWith * 3 / 5, screenHeight);
                updateViewLayout(childView, params);
            }
        }
    }
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

總結

處理螢幕旋轉可以分成三個層次:先用 screenOrientation 開啟自動旋轉,再用 configChanges 與 onConfigurationChanged 攔截旋轉事件避免重複初始化,最後在版面層級透過覆寫 onMeasure 依寬高動態調整控制項排布,做出真正自適應的體驗。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved