前言:
当我们开发APP
时如果不对其做任何处理,启动则会根据主题显示白色或者黑色,而这种情况对于用户来说体验性则不大好,现在根据启动展示黑色、白色可做些优化。
冷启动产生原因主要是由APP
启动流程说起
App启动流程:
1.用户点击
icon
2.系统开始加载和启动应用
3.应用启动:开启空白(黑色)窗口
4.创建应用进程
5.初始化Application
6.启动UI
线程
7.创建第一个Activity
8.解析(Inflater)和加载内容视图
9.布局(Layout
)
10.绘制(Draw
)
而这一系列过程会消耗时间,而总有的时间是避免不了的,从而我们要想办法减少冷启动的时间:
- 1.减少在
Application
中的耗时操作(懒加载)- 2.减少在
onCreate
的耗时操作
Android
为我们提供了 android:windowBackground
的解决方案,我们可以专门为 WelcomeActivity
设置一个背景来避免 创建空白(黑色) 窗口这一步骤的尴尬,而对于 android:windowBackground
又延伸了各种各样的方案。
1. 纯色背景 + 启动图标
<application
android:name=".App"
android:allowBackup="true"
android:icon="${app_icon}"
android:label="${app_name}"
android:theme="@style/ActivityTheme">
<meta-data
android:name="CHANNEL_ID"
android:value="${UMENG_CHANNEL_VALUE}" />
<activity
android:name=".business.basic.ui.WelcomeActivity"
android:theme="@style/WelcomeThemeApp"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
style.xml
<!-- 基本主题 -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- 整个应用activity切换动画主题 -->
<style name="ActivityTheme" parent="@android:style/Theme.Holo.Light">
<item name="android:windowNoTitle">true</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
<!--使用图片的方案-->
<style name="WelcomeThemeApp" parent="ActivityTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/welcome</item>
<!--沉浸-->
<item name="android:windowTranslucentStatus">true</item>
</style>
<!--纯色加启动图标的方案-->
<style name="WelcomeThemeLayer" parent="ActivityTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/welcome_layer_list</item>
welcome_layer_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:angle="45"
android:endColor="@color/basic_blue"
android:startColor="@color/colorAccent" />
</shape>
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/welcome" />
</item>
</layer-list>
参考链接:https://mp.weixin.qq.com/s/kHPy6wjjvZiyP9ZxiLUnTA
https://github.com/aohanyao/AndroidRoad/tree/master/ColdStart
https://github.com/saulmm/onboarding-examples-android