Android 啟動APP時黑屏白屏的三個解決方案,androidapp

來源:互聯網
上載者:User

Android 啟動APP時黑屏白屏的三個解決方案,androidapp

你會很奇怪,為什麼有些app啟動時,會出現一會兒的黑屏或者白屏才進入Activity的介面顯示,但是有些app卻不會如QQ手機端,的確這裡要做處理一下。這裡先瞭解一下為什麼會出現這樣的現象,其實很簡單,簡曆一個簡單的例子就可以理解了。

其實,黑屏或者白屏這裡並不是不正常,而是還沒載入到布局檔案,就已經顯示了window視窗背景,黑屏白屏就是window視窗背景。代碼如下,可以自己寫個小demo就理解了。

@Overrideprotected 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,並結束當前的LauncherActivityIntent intent = new Intent(LauncherActivity.this, MainActivity.class);startActivity(intent);finish();break;default:break;}};};@Overrideprotected 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秒後發送訊息,跳轉到MainActivitymHandler.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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.