Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 啟動時黑屏白屏問題的三種解決方案

編輯:關於 Android 程式設計

你是否留意過這樣一個現象:開啟某些 App 時,畫面會先短暫地閃過一片黑屏或白屏,之後才真正進入 Activity 的介面;而有些 App(例如手機 QQ)卻完全沒有這個尷尬的過渡。這其實是一個可以細節打磨的地方。在動手解決之前,我們先弄清楚它為什麼會發生。

原因並不複雜。所謂的黑屏或白屏,並不是程式出錯,而是在布局檔案尚未載入之前,系統已經先把 window 視窗的背景顯示了出來——這片黑或白,正是 window 的視窗背景色。下面這段程式碼能幫助你直觀理解:只要在 setContentView 之前刻意停頓,就能明顯看到那個「過渡畫面」。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 注意:這裡刻意加入 3 秒睡眠,讓黑屏效果更明顯;正式專案中請移除
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // 在執行 setContentView 之前,畫面顯示的都是 window 的背景色,尚未進入布局
    setContentView(R.layout.activity_launcher);
}

那麼這個 window 背景是在哪裡設定的呢?答案就在 theme 主題裡。以下這段樣式把背景設成了白色,也就是啟動時白屏的來源:

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
</style>

換句話說,只要在 theme 中妥善設定 windowBackground,就能消除啟動時那一瞬間的黑屏或白屏。以下提供三種常見做法。

一、使用 .png 背景圖

直接提供一張背景圖是最直截了當的辦法,但缺點是要為各種解析度準備多張圖片來做适配。除非畫面圖案確實非常複雜、非用圖片不可,否則個人並不特別推薦這種方式。

二、使用 .9.png(NinePatch)背景圖

如果圖案並不算太複雜,可以將它製作成 NinePatch 圖片。這樣只需一張圖就能自動适配各種尺寸的螢幕,何樂而不為?

三、使用 layer-list 製作背景

如果條件允許,我最推薦用 layer-list 的方式來打造背景。前兩種做法都需要載入圖片、佔用記憶體;而 layer-list 相對省記憶體,也能避免因為圖片過多導致 App 體積膨脹。

下面用一個完整範例說明:LaunchActivity 作為啟動頁停留 3 秒後跳轉到 MainActivity,同樣為了放大效果,在 setContentView 之前讓執行緒睡眠 3 秒。

public class LauncherActivity extends Activity {

    public final int MSG_FINISH_LAUNCHERACTIVITY = 500;

    public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_FINISH_LAUNCHERACTIVITY:
                    // 跳轉到 MainActivity,並結束當前的 LauncherActivity
                    Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 隱藏系統標題欄,讓 windowBackground 與介面大小一致,避免錯位
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 注意:這裡的 3 秒睡眠僅為演示黑屏效果,正式專案請移除
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        setContentView(R.layout.activity_launcher);
        // 停留 3 秒後發送訊息,跳轉到 MainActivity
        mHandler.sendEmptyMessageDelayed(MSG_FINISH_LAUNCHERACTIVITY, 3000);
    }
}

activity_launcher.xml 布局很簡單,關鍵在於這裡 LinearLayout 使用的背景 layout_list_start_pic 必須與主題 theme 使用同一張背景,如此才能消除背景不一致造成的跳動感。這一步的效果建議親自跑一次就能體會。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/layout_list_start_pic" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#ffffff"
        android:text="@string/hello_world" />
</LinearLayout>

在 AndroidManifest.xml 中,application 使用的是 AppTheme,而 LauncherActivity 單獨指定了 StartAppTheme。這樣一來,只有啟動頁套用 StartAppTheme,其餘 Activity 仍沿用 AppTheme。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.launcheractivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LauncherActivity"
            android:label="@string/app_name"
            android:theme="@style/StartAppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"></activity>
    </application>
</manifest>

styles.xml 中定義了兩個主題,分別對應一般頁面與啟動頁:

<resources xmlns:android="https://schemas.android.com/apk/res/android">
    <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/white</item>
    </style>

    <style name="StartAppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/layout_list_start_pic</item>
    </style>
</resources>

最後是 layer-list 組成的啟動背景 layout_list_start_pic.xml,它同時定義了整屏底色、中央 logo 與底部圖示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android" >
    <!-- 設定整個螢幕背景為白色 -->
    <item >
        <color android:color="@color/white"/>
    </item>
    <!-- 中間 logo -->
    <item >
        <bitmap
            android:gravity="center"
            android:src="@drawable/ic_launcher" />
    </item>
    <!-- 底部圖表 -->
    <item android:bottom="10dp">
        <bitmap
            android:gravity="bottom|center_horizontal"
            android:src="@drawable/copyright" />
    </item>
</layer-list>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved