一、黑白屏原因
在app启动流程中,当系统加载并启动app时,需要耗费一定的时间。即使这个时间非常短,但是用户依旧会感觉点击App图标的“延迟”。为解决这一问题,Google在App创建的过程中,先展示一个空白页面,让用户体会到点击图标后立马就会有响应。而这个空白页面的颜色,就是根据我们清单文件中配置的主题背景色来决定的,一般默认为白色。
二、解决方案
2.1 修改AppTheme
在应用默认的AppTheme中,设置系统“取消预览空白窗体”为true,或设置为透明窗体。这两种方式实际上都是一种做法:将Theme的背景改为透明,让用户在视觉上无法感觉到黑白屏的存在。但是这种做法治标不治本,在点击图标后,会短暂“卡顿”后才会启动。
// 取消预览
<item name="android:windowDisablePreview">true</item>
// 设置透明背景
<item name="android:windowIsTranslucent">true</item>
2.2 自定义AppTheme
①取代空白窗体部分
以前就是个纯白色或黑色,现在让它成为我们自定义的图片或颜色
styles.xml
<style name="AppTheme.LaunchTheme">
<item name="android:windowBackground">@drawable/launch_layout</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
</style>
launch_layout.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 设置背景颜色 -->
<item android:drawable="@android:color/holo_red_dark"/>
<!-- 设置图片及位置 -->
<item android:top="150dp">
<bitmap android:gravity="top"
android:src="@mipmap/top_img" />
</item>
</layer-list>
②Splash自己的逻辑部分
我们可能会在闪屏页做一些动画或广告
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SplashActivity">
<ImageView
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/bottom_img"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"/>
</RelativeLayout>
先加载预览窗体,等闪屏页加载完成后再将其替换。
设置闪屏页的主题Theme:
<activity android:name=".SplashActivity" android:theme="@style/AppTheme.LaunchTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
这样一来,就会让用户认为这个“时间差”是我们故意为之的。另外,纯色的背景色,在机型适配上能够避免被拉伸。