Android 启动页白屏优化

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

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

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

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

android:theme="@style/AppTheme"

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

<!-- 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

<?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>

2. 将 bg_splash.xml 设为 Window 背景

    <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>

3. 将 bg_splash.xml 设为启动页 Activity 的背景

<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 启动优化

    2024-01-18 20:40:03       68 阅读
  2. Android APP启动优化

    2024-01-18 20:40:03       37 阅读
  3. Android启动优化

    2024-01-18 20:40:03       35 阅读
  4. Android启动优化

    2024-01-18 20:40:03       38 阅读
  5. Android启动优化

    2024-01-18 20:40:03       39 阅读
  6. Android app启动优化 2

    2024-01-18 20:40:03       57 阅读
  7. android 启动优化方向跟踪

    2024-01-18 20:40:03       33 阅读
  8. Android界面启动流程

    2024-01-18 20:40:03       43 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 20:40:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 20:40:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 20:40:03       82 阅读
  4. Python语言-面向对象

    2024-01-18 20:40:03       91 阅读

热门阅读

  1. What is `XSS` does?

    2024-01-18 20:40:03       55 阅读
  2. C#将货币金额数字转大写汉字

    2024-01-18 20:40:03       52 阅读
  3. MySQL8.0.26-Linux版安装

    2024-01-18 20:40:03       63 阅读
  4. 网络工程师:计算机基础知识面试题(四)

    2024-01-18 20:40:03       47 阅读
  5. tinyxml2

    tinyxml2

    2024-01-18 20:40:03      46 阅读
  6. 设计模式——中介者模式

    2024-01-18 20:40:03       55 阅读
  7. DEJA_VU3D - Cesium功能集 之 119-三维热力图

    2024-01-18 20:40:03       63 阅读