從我學(xué)習(xí)寫第一個(gè)android項(xiàng)目以來,我都是這樣寫著啟動(dòng)界面:
- 在里面做一些事,比如:第一次啟動(dòng)時(shí)拷貝數(shù)據(jù)。
- 然后讓啟動(dòng)界面一定待夠一定時(shí)間,比如兩秒三秒的。
就在前兩天我打開app的時(shí)候,我感覺啟動(dòng)界面的時(shí)間太長了,而且為什么會先白屏一下然后進(jìn)入啟動(dòng)界面。很多app都有啟動(dòng)界面,也有很多app沒有啟動(dòng)界面,但是我發(fā)現(xiàn)這些沒有啟動(dòng)界面的app,當(dāng)我點(diǎn)擊桌面那個(gè)icon的時(shí)候,也會先白屏一下,然后進(jìn)入主頁。
然后我決定做兩件事:
- 做完啟動(dòng)界面的事就進(jìn)入首頁,不故意睡了。
- 消滅白屏(跟主題設(shè)置的關(guān)系也有可能是黑屏),在點(diǎn)擊桌面app icon的瞬間開啟啟動(dòng)界面,因?yàn)槲尹c(diǎn)了美團(tuán),發(fā)現(xiàn)它是秒開的。。
如何消滅白屏
- 刪除啟動(dòng)界面的xml布局,刪除setContentView。
- 在res/drawable里寫一個(gè)這種玩意:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:id="@+id/bitmap_splash"
android:src="@mipmap/splash_bg">
</bitmap>
</item>
<item
android:top="@dimen/splash_logo_marginTop">
<bitmap
android:gravity="top"
android:src="@mipmap/splash_logo">
</bitmap>
</item>
<item
android:bottom="80dp">
<bitmap
android:gravity="bottom"
android:src="@mipmap/splash_word">
</bitmap>
</item>
</layer-list>
item有drawable屬性,但是不能接收mipmap參數(shù),所以我又包了bitmap。
- 在style里配置主題,我這里AppBaseTheme的parent是Theme.AppCompat.Light.NoActionBar,然后還有其他的一些配置。
<style name="Splash" parent="AppBaseTheme">
<item name="android:windowBackground">@drawable/splash</item>
</style>
- 在manifest中splash的activity標(biāo)簽中配置主題:
...
android:theme="@style/Splash"
...
- 啟動(dòng)一下看看效果吧。
網(wǎng)上搜啟動(dòng)消除白屏的方法,有設(shè)置啟動(dòng)界面主題的背景為透明的,splash的xml布局還和以前一樣,這樣確實(shí)不白屏了,但是點(diǎn)擊桌面上的icon開始會等一會splash才會出現(xiàn),體驗(yàn)也不好。