Android 启动页白屏解决方案

作者:一只修仙的猿 时间:2023-11-06 02:54:02 

当我们打开app的时候是不是会有一瞬间的白屏然后再进入主活动,虽然这并不会造成什么不好的后果,但是感觉用户体验就不是很好。像网易云音乐等等,打开一瞬间就显示了他们的loge,无缝衔接,没有白屏,怎么做到的呢?

一开始我的思路是这样的。可能是因为我们的主活动逻辑太多,所以加载会变慢,导致显示白屏。如果使用一个只显示一张本地图片的活动,那会不会就不会显示白屏了呢。话不多说我们尝试一下:

Activity中的代码:


/**
* 启动页,显示倾旅的logo,停顿2秒后跳转
*/
public class LunchActivity extends AppCompatActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_lunch);

//开启子线程进行停顿。如果在主线程停顿的话,会造成主页面卡死,所以在子线程sleep两秒后跳转
   new Thread(new Runnable() {
     @Override
     public void run() {
       try {
         Thread.sleep(1000);
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
       start();
       LunchActivity.this.finish();
     }
   }).start();
 }
 //跳转到主页面
 private void start(){
   Intent intent = new Intent(LunchActivity.this,MainActivity.class);
   startActivity(intent);
 }
}

layout中的代码:


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
 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"
 android:background="#e74b37"
 tools:context=".LunchActivity">

<ImageView
   android:id="@+id/imageView5"
   android:layout_width="80dp"
   android:layout_height="80dp"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintEnd_toEndOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintTop_toTopOf="parent"
   app:layout_constraintVertical_bias="0.31"
   app:srcCompat="@drawable/icon" />
</android.support.constraint.ConstraintLayout>

这里简单指定一个imageView来显示一张图片。并把背景设置为橘色

最后再把启动页活动设置为主活动:


<activity android:name="com.example.qinglv.LunchActivity">
     <intent-filter>
       <action android:name="android.intent.action.MAIN" />
       <category android:name="android.intent.category.LAUNCHER" />
     </intent-filter>
   </activity>

一切想的很好,完成后打开一看,还是会白屏,怎么回事?

活动的加载都是需要时间的,比较简单的活动时间会少点,但是以然会有一瞬间的白屏。那这个白屏到底是什么?就是每个活动的背景。当打开一个活动的时候,因为还没加载出内容,所以显示的就只是背景,所以我们只需要,改变这个背景,设置为我们需要的一个logo照片即可。怎么设置呢?

  • 背景是在主题中指定的,首先设置一个主题,把背景改成我们要的。一般和我们的启动页保持一致,这样的话就不会看起来像两个启动页一样。也可以像网易云音乐那样,背景设置成logo,但是启动页是放广告,但是这会影响用户体验(为了收入打点广告也是可以理解的)。看代码:

在res-value-styles:


<style name="NewAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  <!-- Customize your theme here. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="android:windowBackground">@color/colorPrimary</item>
  <item name="colorAccent">@color/colorAccent</item>
</style>

重点是这句<item name="android:windowBackground">@color/colorPrimary</item>
这里我指定的是一种颜色你们也可以指定一张图片

  • 再给启动页活动指定主题:

在:AndroidManifest:


<activity android:name="com.example.qinglv.LunchActivity"
    android:theme="@style/NewAppTheme">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>

重点是这句android:theme="@style/NewAppTheme"

然后再打开的时候,就会发现不会了。原本显示的白屏变成了我们设置好的图片。

来源:https://qwerhuan.gitee.io/2020/04/21/android/android-an-zhuo-qi-dong-ye-jie-jue-da-kai-ying-yong-xian-shi-bai-ping-qing-kuang/

标签:Android,启动页,白屏
0
投稿

猜你喜欢

  • 详解Android Lint的原理及其使用

    2022-12-23 16:05:47
  • Log4j.properties配置及其使用

    2023-05-14 21:06:18
  • 了解Java虚拟机JVM的基本结构及JVM的内存溢出方式

    2023-02-20 03:08:51
  • java struts2学习笔记之线程安全

    2022-08-07 00:13:07
  • 详解Spring循环依赖的解决方案

    2022-05-29 13:14:57
  • android桌面悬浮窗显示录屏时间控制效果

    2022-04-21 14:54:12
  • C#实现网络小程序的步骤详解

    2023-08-17 18:16:37
  • java编写Http服务器下载工具

    2021-11-08 08:07:38
  • mybatis plus使用redis作为二级缓存的方法

    2023-11-19 08:34:58
  • java并发编程工具类JUC之ArrayBlockingQueue

    2023-07-04 21:02:20
  • Java和C++通过new创建的对象有何区别?

    2022-02-07 10:06:10
  • 树莓派.GPRS.短信接收器

    2021-11-01 08:34:31
  • C++类与对象深入之构造函数与析构函数详解

    2021-06-29 13:44:44
  • Java实现二叉搜索树的插入、删除功能

    2023-07-15 20:54:53
  • SpringBoot整合log4j2日志的实现

    2021-11-14 23:41:01
  • Android5.0中多种水波纹效果的实现代码

    2023-09-23 04:42:53
  • MyBatis使用动态表或列代码解析

    2023-06-13 07:57:40
  • 剑指Offer之Java算法习题精讲链表专项训练

    2023-11-29 16:31:48
  • c#获取数组中最大数的值

    2022-07-20 07:49:02
  • Android App后台服务报告工作状态实例

    2023-04-26 19:10:34
  • asp之家 软件编程 m.aspxhome.com