很簡(jiǎn)單
AndroidManifest里對(duì)應(yīng)activity添加屬性android:theme="@style/AppSplash"
<activity
android:name="com.senyint.edu.college.stu.view.activity.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppSplash">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
@style/AppSplash:
<style name="AppSplash" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/splash</item>
</style>
@drawable/splash:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="@mipmap/splash"/>
</item>
</layer-list>
如此這樣便可以了囤躁,當(dāng)然這只是給用戶的一種感覺逃糟,并不是真的“秒開”app猛铅。
在一個(gè)Activity打開時(shí)冕屯,如果該Activity所屬的Application還沒有啟動(dòng)此衅,那么系統(tǒng)會(huì)為這個(gè)Activity創(chuàng)建一個(gè)進(jìn)程,在進(jìn)程的創(chuàng)建和初始化中为流,會(huì)消耗一些時(shí)間烫映,在這個(gè)時(shí)間里,WindowManager會(huì)先加載APP里的主題樣式里的窗口背景(windowBackground)作為預(yù)覽元素兢交,然后才去真正的加載布局薪捍。而我上文所做的就是把啟動(dòng)的界面放在style的windowBackground配置里作為預(yù)覽元素呈現(xiàn)給用戶。