android應用啓動時白屏(冷啓動)

圖標點擊啓動應用的時候都有一段時間的白屏,這會給人一種卡頓的感受。注意看了下,手機上有很多應用也有這樣的問題。不過微信、百度地圖這些應用沒有這個問題。android

解決的辦法有不少,其中最簡單的一種:git

咱們能夠定義一個簡單的主題樣式,在樣式中將windowBackground從新設置(android默認的主題樣式將該值設置爲白色,因此會出現短期的白屏)github

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    </style>

    <style name="Launcher" parent="AppTheme">
        <item name="android:windowBackground">@drawable/window_background</item>
    </style>

    <style name="AppThemeRed" parent="AppTheme">
        <item name="colorPrimary">@color/color_primary_red</item>
        <item name="colorPrimaryDark">@color/color_primary_red_dark</item>
    </style>

這裏定義了一個Launcher的主題樣式,咱們能夠在maifest中引用該主題,這裏設置的windowBackground會將全部antivity中的背景設置爲指定背景。因此爲了使activity中的背景恢復爲默認白色,須要在activity中從新設置主題:微信

setTheme(R.style.AppThemeRed);

注意這句話應該寫在super.onCreate()前面。post

背景的drawable能夠是這樣的:google

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque">
    <item android:drawable="@color/launcher_back_color"/>
    <item>
        <bitmap
            android:src="@mipmap/ic_launcher"
            android:gravity="center"/>
    </item>
</layer-list>

具體參考:https://plus.google.com/+AndroidDevelopers/posts/Z1Wwainpjhdcode

項目參考:https://github.com/buobao/NiceGirl.gitxml

其餘一些解決方案參考:http://saulmm.github.io/avoding-android-cold-startsip

相關文章
相關標籤/搜索