SplashActivity的实现步骤
由于在APP启动页面加载时,会有白屏闪过,用这种方法加载启动页面的图片会避免此问题,达到秒开的效果。
1 在drawable文件夹下建立splash.xml.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@color/dark_gray"></item>
<item>
<bitmap android:src="@mipmap/loading"
android:gravity="center"/>
</item>
</layer-list>
2 在样式文件style.xml中为SplashActivity建立一个theme.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>
</resources>
3 在AndroidManifest.xml中定义SplashActivity的theme为splash_theme.
<activity android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
4 为了保证启动速度,SplashActivity不需要实现setContentView()方法。
如果你的SplashActivity设置了layout文件,那么其在app完全初始化完成后才会显示,带来一定的耗时。这里直接以theme作为SplashActivity展示的UI,减少了加载时间,能达到秒开的效果。使用该启动方式亦可以避免有些app点开时出现的白屏问题。