android冷启动解决方案

关于android splash的页面,相信很多android开发都是很熟悉的,就是点击应用图标后第一个出现的那个界面。但是你可能会发现这样一种情况,第一次启动应用的时候,会出现一瞬间的黑屏或者白屏,之后才进入你写的splash界面。这个主要就是android的冷启动造成的,所有的应用都不可避免的会出现这样的情况。 那么有什么可以解决的方案呢?当然是有的。
首先先了解一下冷启动,其实就是第一次启动运用或者说一段时间未使用导致应用被系统kill,这个过程,由于从启动应用到重新布局绘制渲染都要花费一定的时间,从而出现了上面所说的短暂的黑屏或者白屏。

解决方法:

可以给你的splash的Activity页面设置主题,主题里面设置android:windowBackground的值。如下

<!— Base application theme.> 
<style name=“AppTheme” parent=“Theme.AppCompat.Light.NoActionBar”>  
<!— Customize your theme here.>         
    <item name=“colorPrimary”>@color/colorPrimary</item> 
    <item name=“colorPrimaryDark”>@color/colorPrimaryDark</item> 
    <item name=“colorAccent”>@color/colorAccent</item>
</style>     
<!—冷启动优化—>    
 <style name=“AppWelcome” parent=“AppTheme”>  
     <item name=“android:windowBackground”>@drawable/bg_splash</item> 
</style> 

这里假设splash页面是一个图片bg_splash.png,通过上面的设置,可以使得白屏或者黑屏的部分被上面设置的图片背景替代,无缝衔接。 如果你想给白屏或者黑屏部分替代为纯色+图标的样式,也很简单。将上面的bg_splash图片替换成下面的xml文件引用即可。

xml version=1.0” encoding=“utf-8?> 
<layer-list xmlns:android=“http://schemas.android.com/apk/res/android“>       
    <item android:drawable=@color/colorPrimary” /><item>
    <bitmap android:gravity=“center” android:src=@mipmap/ic_launcher”/></item> 
</layer-list>

欢迎关注本人公众号和小程序,谢谢
在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/stonezry/article/details/106229054