欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android冷启动实现app秒开的实现代码

程序员文章站 2023-12-15 10:03:28
本文介绍了android冷启动实现app秒开的实现代码,分享给大家,具体如下: androidmanifest里对应activity添加属性android:theme...

本文介绍了android冷启动实现app秒开的实现代码,分享给大家,具体如下:

androidmanifest里对应activity添加属性android:theme="@style/appsplash"

<activity
      android:name="com.senyint.edu.college.stu.view.activity.splashactivity"
      android:screenorientation="portrait"
      android:theme="@style/appsplash">
      <intent-filter>
        <action android:name="android.intent.action.main"/>
        <category android:name="android.intent.category.launcher"/>
      </intent-filter>

    </activity>

@style/appsplash:

<style name="appsplash" parent="theme.appcompat.light.noactionbar">
    <item name="android:windowfullscreen">true</item>
    <item name="android:windowbackground">@drawable/splash</item>
  </style>

@drawable/splash:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item>
    <bitmap
      android:src="@mipmap/splash"/>
  </item>
</layer-list>

如此这样便可以了,当然这只是给用户的一种感觉,并不是真的“秒开”app。

在一个activity打开时,如果该activity所属的application还没有启动,那么系统会为这个activity创建一个进程,在进程的创建和初始化中,会消耗一些时间,在这个时间里,windowmanager会先加载app里的主题样式里的窗口背景(windowbackground)作为预览元素,然后才去真正的加载布局。而我上文所做的就是把启动的界面放在style的windowbackground配置里作为预览元素呈现给用户。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: