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>
相关推荐
早上好啊! 树哥1 小时前
安卓开发:清除缓存并重启,删除指定路径下的文件缓存
android·缓存
h***34631 小时前
Nginx 缓存清理
android·前端·后端
Tom4i2 小时前
Perfetto 快速上手
android·性能优化·perfetto
fatiaozhang95272 小时前
创维桌面云终端-创维LB2002-黑盒-国科gk6323-2+8G-短接强刷固件包
android·电视盒子·刷机固件·机顶盒刷机·创维lb2002·创维lb2002-黑盒·创维lb2002-白盒
q***71858 小时前
MySQL--》如何在MySQL中打造高效优化索引
android·mysql·adb
IT痴者10 小时前
《PerfettoSQL 的通用查询模板》---Android-trace
android·开发语言·python
游戏开发爱好者810 小时前
iOS IPA 上传工具全面解析,从 Transporter 到开心上架(Appuploader)命令行的高效上架实践
android·ios·小程序·https·uni-app·iphone·webview
alexhilton13 小时前
Jetpack Compose中的阴影艺术
android·kotlin·android jetpack
百***618716 小时前
Spring的构造注入
android·java·spring
Tom4i16 小时前
Kotlin 中的 inline 和 reified 关键字
android·开发语言·kotlin