在项目中有没有觉得自己App启动慢,第一次启动的时候会有0.5s的白屏,项目经理有没有提出这个细节呢?或许你没有注意,或许你已经解决了,但是App冷启动都是绕不开的话题。
产生的原因:
还没加载到布局文件,就已经显示了窗口窗口背景,黑屏白屏就是窗口窗口背景。布局还没加载出来,窗口是白色,那怎么解决呢?
解决方案:
1.让窗口透明,启动的时候就看不到白色窗口了。
2.给窗口加上背景,一进来就显示出来啊,那白屏不就解决了吗?
先学会常用的Theme主题功能:
Activity显示为对话框模式
android:theme=”@android:style/Theme.Dialog”
不显示应用程序标题栏
android:theme=”@android:style/Theme.NoTitleBar”
不显示应用程序标题栏,并全屏
android:theme=”@android:style/Theme.NoTitleBar.Fullscreen”
背景为白色
android:theme=”Theme.Light ”
白色背景并无标题栏
android:theme=”Theme.Light.NoTitleBar”
白色背景,无标题栏,全屏
android:theme=”Theme.Light.NoTitleBar.Fullscreen”
背景黑色
android:theme=”Theme.Black”
黑色背景并无标题栏
android:theme=”Theme.Black.NoTitleBar”
黑色背景,无标题栏,全屏
android:theme=”Theme.Black.NoTitleBar.Fullscreen”
用系统桌面为应用程序背景
android:theme=”Theme.Wallpaper”
用系统桌面为应用程序背景,且无标题栏
android:theme=”Theme.Wallpaper.NoTitleBar”
用系统桌面为应用程序背景,无标题栏,全屏
android:theme=”Theme.Wallpaper.NoTitleBar.Fullscreen”
透明背景
android:theme=”Theme.Translucent”
透明背景并无标题
android:theme=”Theme.Translucent.NoTitleBar”
透明背景并无标题,全屏
android:theme=”Theme.Translucent.NoTitleBar.Fullscreen”
面板风格显示
android:theme=”Theme.Panel ”
1.窗口透明做法:将该主题设置到启动activity。 使用<item name=”android:windowBackground”>@android:color/transparent</item>设置背景为透明。
<style name="TransluteTheme" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:screenOrientation">portrait</item>
</style>
透明方法效果图:可以看出,点击启动图标之后,虽然立即启动了,但是窗口透明,没有白屏,但是缺点就是启动速度慢,没有达到秒速启动的效果。
gif
2.伪布局做法(主流做法):将该主题设置到启动activity。 使用<item name=”android:windowBackground”>@mipmap/splashbg</item>设置默认图片为splashbg。
<style name="SplashTheme" parent="AppTheme">
<item name="android:windowBackground">@mipmap/splashbg</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
默认背景图:点击启动图标立即加载窗口,立即显示背景图,达到秒速启动效果。(ps:这张图是我临时做的一张,不满效果,不要打我)
giphy.gif
好了,功能实现了。喜欢我就点我吧,博客持续更新中,最实用的项目功能就在其中。