我们在设置app启动页面的时候一般会在app启动的第一个页面的contentview设置背景图片,但是这样做的话,当你点击手机桌面的icon到显示出你contentview设置的背景,中间会有一段时间的黑屏或者白屏,解决方法就是下面代码:
<activity
android:name=".splash.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Launcher">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<style name="AppTheme.Launcher">
<item name="android:windowBackground">@drawable/bg_app</item>
</style>
然而你会发现在许多手机,特别是刘海屏什么的,启动页面会有拉伸导致的闪烁情况,这就需要使用下面的方法完美解决:
<style name="AppTheme.Launcher">
<item name="android:windowBackground">@drawable/page_splash</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/M_bg" />
</shape>
</item>
<item android:bottom="80dp">
<bitmap android:src="@drawable/icon_splash_logo"
android:gravity="bottom|center_horizontal"/>
</item>
</layer-list>
注意上面设置的bottom="80dp"要和SplashActivity的 XML 文件中图片位置android:layout_marginBottom="80dp"保持一致
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@color/M_bg"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:scaleType="centerCrop"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="80dp"
android:src="@drawable/icon_splash_logo"
android:layout_height="wrap_content" />
</RelativeLayout>