android App冷启动优化,实现点击App Logo,立马又相应,而且不会白屏和黑屏;优化用户体验!
参考:https://www.cnblogs.com/xunzhi/p/5794793.html
第一步:launcher_background.xml (drawable)
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 启动页面的背景图片 (图片大小需要注意,我这边图片的宽高比例是1:2) -->
<item android:drawable="@mipmap/beijing_whole" />
<!-- 整体的背景颜色 -->
<!--<item android:drawable="@android:color/transparent" />-->
<!--<item >-->
<!--<bitmap android:src="@mipmap/beijing_whole"/>-->
<!--</item>-->
</layer-list>
第二步:styles.xml
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--将ActionBar隐藏,这里使用ToolBar-->
<item name="windowActionBar">false</item>
<!-- 使用 API Level 22以上编译的话,要拿掉前綴字 -->
<item name="windowNoTitle">true</item>
<item name="alertDialogTheme">@style/alert_dialog_theme</item>
</style>
<!-- 启动页面设置成全屏展示和windowBackground -->
<style name="AppTheme.Launcher">
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">@drawable/launcher_background</item>
</style>
第三步:manifest.xml
<application
……………………
android:theme="@style/AppTheme"
………………>
<activity android:name="………….WelcomeActivity"
android:theme="@style/AppTheme.Launcher"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
第四步:activity_welcome.xml
设置启动页面的背景为透明色:(因为启动页面已经设置了theme背景,所以这里再次设置背景多余,也有可能拉伸图片的效果)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent">
</LinearLayout>
第五步:MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
// 冷启动优化后重新设回原来的主题
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
android studio,使用flutter开发,冷启动优化的处理:
1》、flutter工程,对android studio已经做过处理的;在style.xml里:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
<!-- Show a splash screen on the activity. Automatically removed when
Flutter draws its first frame -->
<item name="android:windowBackground">@drawable/launch_background</item>
</style>
</resources>
2》、在对应的 launch_background.xml里:
<?xml version="1.0" encoding="utf-8"?>
<!-- Modify this file to customize your launch splash screen -->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--<item android:drawable="@android:color/holo_red_dark" />-->
<item android:drawable="@mipmap/beijing_whole"/>
</layer-list>