Android 啓動APP時黑屏白屏的三個解決方案

你會很奇怪,爲什麼有些app啓動時,會出現一會兒的黑屏或者白屏才進入Activity的界面顯示,但是有些app卻不會如QQ手機端,的確這裏要做處理一下。這裏先了解一下爲什麼會出現這樣的現象,其實很簡單,簡歷一個簡單的例子就可以理解了。
 
其實,黑屏或者白屏這裏並不是不正常,而是還沒加載到佈局文件,就已經顯示了window窗口背景,黑屏白屏就是window窗口背景。代碼如下,可以自己寫個小demo就理解了。
 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 注意:添加3秒睡眠,以確保黑屏一會兒的效果明顯,在項目應用要去掉這3秒睡眠
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // 在這裏之前,黑屏或者白屏都是window的背景顏色,是窗口背景,還沒到界面的佈局呢,要執行setContentView後才顯示佈局
    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>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
所以,在theme設置windowBackground就可以解決啓動時白屏黑屏一會兒了,下面提供三種解決方案:
 
一、提供.png背景圖
 
提供背景圖是解決的一個方法,但是要適配各種屏幕,提供很多張圖片。除非圖片非常複雜只能用背景圖了就用這種方法吧,否則個人不建議。
 
二、提供.9.png(NinePatch)背景圖片
 
如果圖片不是很複雜,可以做成NinePatch圖片,那就直接製作NinePatch圖片,提供一張就可以適配任何手機,何樂而不爲呢。
 
三、使用Layout-list製作背景圖片
 
如果可以使用這種方式,推薦使用這種Layout-list製作背景圖片。前2種都是使用圖片佔用內存啊,使用Layout-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和界面activity_main的大小一樣,顯示在屏幕不會有錯位(去掉這一行試試就知道效果了)
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
         
        // 注意:添加3秒睡眠,以確保黑屏一會兒的效果明顯,在項目應用要去掉這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="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://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使用的theme是AppTheme,而LauncherActivity使用的主題是StartAppTheme。這樣做的效果是隻要LauncherActivity使用StartAppTheme主題,其他Activity都是用AppTheme主題哦。
 
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://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,2個主題設置
 
 
<resources xmlns:android="http://schemas.android.com/apk/res/android">
 
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light.NoTitleBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>
 
    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@color/white</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    <style name="StartAppTheme" parent="AppBaseTheme">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/layout_list_start_pic</item>
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
</resources>
layout_list_start_pic.xml 啓動頁面使用這個作爲背景圖片
 
 
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://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>
發佈了49 篇原創文章 · 獲贊 4 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章