最近项目忙完,想来优化APP的一些细节,由于每次APP启动都会白屏一下,才会显示出启动页的实际效果图,导致APP体验不好,所以网上搜集资料,防止健忘,特记录于此。
网上大神解说白屏原因,是因为点击APP图标,首次启动,系统加载启动页,到导致浪费一些时间,但是系统为了体验友好,会根据APP主题,先设置启动为白色/黑色背景,这就是大家所看到的白屏问题。
知道了上面的所说的原因之后,问题也就很好解决了,解决步骤:
1、APP的启动activity定义一个主题,主题根据自己APP自身需求,设置启动图
主题样式:
定义启动背景图 splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 背景颜色 -->
<item android:drawable="@color/white" />
<item>
<!-- 图片 -->
<bitmap
android:gravity="center"
android:src="@drawable/start_bg" />
</item>
</layer-list>
启动Activity的主题:
<style name="SplashTheme" parent="android:Theme.Light.NoTitleBar">
<!-- 启动页背景 -->
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowFullscreen">true</item>
<!-- <item name="android:windowIsTranslucent">true</item> --> <!-- 透明背景 -->
</style>
AndroidManifest.xml 设置主题:
<activity
android:name=".activity.ui.StartFlashActivity"
android:theme="@style/SplashTheme"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
2、尽量不让启动Activity去调用setContentView(int resLayout);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}