一、黑白屏原因
在app啓動流程中,當系統加載並啓動app時,需要耗費一定的時間。即使這個時間非常短,但是用戶依舊會感覺點擊App圖標的“延遲”。爲解決這一問題,Google在App創建的過程中,先展示一個空白頁面,讓用戶體會到點擊圖標後立馬就會有響應。而這個空白頁面的顏色,就是根據我們清單文件中配置的主題背景色來決定的,一般默認爲白色。
二、解決方案
2.1 修改AppTheme
在應用默認的AppTheme中,設置系統“取消預覽空白窗體”爲true,或設置爲透明窗體。這兩種方式實際上都是一種做法:將Theme的背景改爲透明,讓用戶在視覺上無法感覺到黑白屏的存在。但是這種做法治標不治本,在點擊圖標後,會短暫“卡頓”後纔會啓動。
// 取消預覽
<item name="android:windowDisablePreview">true</item>
// 設置透明背景
<item name="android:windowIsTranslucent">true</item>
2.2 自定義AppTheme
①取代空白窗體部分
以前就是個純白色或黑色,現在讓它成爲我們自定義的圖片或顏色
styles.xml
<style name="AppTheme.LaunchTheme">
<item name="android:windowBackground">@drawable/launch_layout</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
</style>
launch_layout.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 設置背景顏色 -->
<item android:drawable="@android:color/holo_red_dark"/>
<!-- 設置圖片及位置 -->
<item android:top="150dp">
<bitmap android:gravity="top"
android:src="@mipmap/top_img" />
</item>
</layer-list>
②Splash自己的邏輯部分
我們可能會在閃屏頁做一些動畫或廣告
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<ImageView
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/bottom_img"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
先加載預覽窗體,等閃屏頁加載完成後再將其替換。
設置閃屏頁的主題Theme:
<activity android:name=".SplashActivity" android:theme="@style/AppTheme.LaunchTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
這樣一來,就會讓用戶認爲這個“時間差”是我們故意爲之的。另外,純色的背景色,在機型適配上能夠避免被拉伸。