简介
当打开一个Activity时,如果这个Activity所属的Application还没启动,那么系统会这个Activity创建一个进程(每创建一个进程都会调用一次Application,所以Application的onCreate()方法可能会被调用多次)。在进程的创建和初始化中,势必会消耗一些时间,在这个时间里,WindowManager会先加载APP里的主题样式里的窗口背景(windowBackground)作为预览元素,然后才去真正的加载布局,如果这个时间过长,而默认的背景又是黑色或者白色,这样会给用户造成一种错觉,这个APP很卡,很不流畅,自然也影响了用户体验。
因此,我们可以自定义设计windowBackground来优化用户体验。
<activity
android:name=".activity.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/LaunchTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<style name="LaunchTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/bg_launcher</item>
</style>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<!-- The background color, preferably the same as your normal theme -->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/white"/>
</shape>
</item>
<item>
<bitmap
android:gravity="bottom|center"
android:src="@drawable/ic_splash_bottom" />
</item>
</layer-list>
这样就达到了自定义App启动帧的效果了。