本文首发在我的个人博客: http://www.geekqian.com/post/9d4eac9b.html
转载请注明出处
要优化 app 的启动体验, 首先在清单文件中第一个启动的 SplashActivity 使用了 theme 的方式.
<activity
android:name="com.ingdan.xxx.ui.activity.SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
在 style.xml 文件中定义
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowFullscreen">true</item>
</style>
注意 @drawable/splash 可以是一张图片, 也可以是定义在 drawable 文件夹中的一个 splash.xml 文件.
我这里使用的是 xml 文件的方式.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorWhite"/>
<item>
<bitmap android:src="@drawable/splash_default"
android:gravity="center"
/>
</item>
</layer-list>
当时好像是出现了一些问题才这么写. 具体啥问题我想不起来了, 反正这样没毛病.. 哈
另外接入 bugly 后发现了这里报了一个 XmlPullParserException 的问题, 查了一下应该是之前这个 layer-list 的 item 中定义了 @drawable/splash 这种方式导致的, 之前 splash 图片的名字就叫 splash , 导致跟 splash.xml 冲突. 所以改为了 splash_default就搞定了.