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>
相关推荐
_祝你今天愉快9 分钟前
Java-JVM探析
android·java·jvm
飞天卡兹克33 分钟前
forceStop流程会把对应进程的pendingIntent给cancel掉
android
Monkey-旭8 小时前
Android Bitmap 完全指南:从基础到高级优化
android·java·人工智能·计算机视觉·kotlin·位图·bitmap
Mike_Wuzy13 小时前
【Android】发展历程
android
开酒不喝车13 小时前
安卓Gradle总结
android
阿华的代码王国14 小时前
【Android】PopupWindow实现长按菜单
android·xml·java·前端·后端
稻草人不怕疼16 小时前
Android 15 全屏模式适配:A15TopView 自定义组件分享
android
静默的小猫16 小时前
LiveDataBus消息事件总线之二-(不含反射和hook)
android
~央千澈~17 小时前
05百融云策略引擎项目交付-laravel实战完整交付定义常量分文件配置-独立建立lib类处理-成功导出pdf-优雅草卓伊凡
android·laravel·软件开发·金融策略
_一条咸鱼_17 小时前
Android Runtime冷启动与热启动差异源码级分析(99)
android·面试·android jetpack