Android APP启动白屏优化

概述

android开发者应该都有这样的体会:开发到一定的阶段,包变得很大了,每次启动APP的时候,总是有白屏一闪而过(白屏的时间和包的大小成正比),这篇文章就是解决这个问题.

上代码

第一步:在style文件中自定义一个customTheme,继承启动页的theme,重写windowBackground属性设为自定义的一张启动图片

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!--parent设置为启动页的theme-->
    <style name="AppTheme.customeTheme" parent="AppTheme">
        <item name="android:windowBackground">@drawable/wx_launch</item>
    </style>

</resources>



第二步:在manifest文件中将启动页(activity)的theme设为自定义的customTheme.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.xiao.custom_launch">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!--启动activity的theme设为自定义的customeTheme-->
        <activity android:name=".MainActivity"
            android:theme="@style/AppTheme.customeTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

简单的两步,就可以将启动时一闪而过的白屏替换为自己的图片(比如带公司logo的图片).

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容