Android 启动页白屏优化

转自:解决 Android APP 启动页白屏问题及如何实现全屏显示_android 启动白屏-CSDN博客

一、白屏原因分析

其实,白屏现象很容易理解,在冷启动一个 APP 的时候,启动页还没完成布局文件的加载,此时显示的是 Window 窗口背景,我们看到的白屏就是 Window 窗口背景。

java 复制代码
@Override
protected void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState);
    // 在加载布局之前,显示的是 window 的背景
    setContentView(R.layout.activity_launcher);
 
}

Window 背景的是由 Application theme 决定的,通过设置 AndroidManifest.xml 文件里面 <application> 属性实现:

java 复制代码
android:theme="@style/AppTheme"

如下所示:背景色是白色背景,启动时就会显示白屏。

java 复制代码
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@color/white</item>
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

其实看到这里,我们就知道怎么来解决白屏问题了,我们可以将 Window 的背景和 APP 启动页的背景设置成一样的,从视觉体验上让用户感觉一点开 APP 显示就是启动页面

二、解决白屏方案

方案一、提供 .png 背景图

如果内容布局比较复杂,可以采用背景图片的方式,但是由于存在不同尺寸和像素密度的屏幕,可能需要提供多张不同的背景图来适配各种屏幕,以避免图片拉伸变形。

如果图片不复杂,可以采用 .9.png 图片,提供一张图片就可以适配任何手机。

方案二、使用 Layout-list 制作背景

  1. 制作替代白屏的背景:bg_splash.xml
java 复制代码
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape>
            <solid android:color="#FFFFFF"/>
        </shape>
    </item>
    <!-- 顶部边距200dp -->
    <item
        android:top="200dp">
        <bitmap
            android:gravity="top|center_horizontal"
            android:src="@mipmap/learn" />
    </item>
</layer-list>
  1. 将 bg_splash.xml 设为 Window 背景
java 复制代码
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- has incuded in parent style -->
        <!--
        <item name="android:windowNoTitle">true</item>
        -->
        <!-- 窗口状态栏颜色可以设置为和背景色一致 -->
        <item name="colorPrimaryDark">@color/white</item>
        <!-- 窗口的背景,替代启动时候出现的白屏 -->
        <item name="android:windowBackground">@drawable/bg_splash</item>
    </style>
  1. 将 bg_splash.xml 设为启动页 Activity 的背景
java 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg_splash" >
    // 其他布局省略,你可以在背景布局上面添加其他内容
</LinearLayout>
相关推荐
javaGHui5 分钟前
安卓传感器横竖屏切换
android·经验分享·笔记
用户69371750013847 分钟前
21.Kotlin 接口:接口 (Interface):抽象方法、属性与默认实现
android·后端·kotlin
QING61817 分钟前
Jetpack Compose 中 Flow 收集详解 —— 新手指南
android·kotlin·android jetpack
泓博19 分钟前
Android截屏汇报问题
android
_李小白24 分钟前
【Android FrameWork】第二十一天:AudioFlinger
android
向葭奔赴♡28 分钟前
Android SharedPreferences实战指南
android·java·开发语言
愤怒的代码29 分钟前
一个使用 AI 开发的 Android Launcher
android
北京自在科技40 分钟前
Find Hub迎来重大升级,UWB技术实现厘米级精准定位,离线追踪覆盖更广
android·google findhub
悠哉清闲1 小时前
SoundPool
android