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>

還有一張圖片copyright.png

好了現在提供下效果圖吧

啟動時,黑屏白屏改造成這圖片樣子,哈哈~

啟動完成后加裝的LauncherActivity界面

調整到MainActivity界面

 就這樣了,本來想給gif動畫的,用微信拍下來,澳澳不知道怎么發到電腦嗚嗚~~~后來才知道可以發到網頁微信。。。

 下載鏈接遲點發上來,哎呀媽的,要睡覺了~~·~~

若本文對你有幫助請點點贊支持,有疑問請留言共同探討~

本文鏈接:http://www.cnblogs.com/liqw/p/4263418.html

下載地址:http://download.csdn.net/detail/lqw770737185/8411705


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM