Android splash的实现

一、Splash的作用

最近做flutter项目,由于app启动时需要初始化flutter引擎,这个过程比较耗时,会有较长时间的白屏(或黑屏,跟设置的application、activity的theme有关),用户体验不太好,于是需要加一个splash页面。
那什么时候需要用到splash呢?1、启动阶段有耗时的初始化操作,影响到了用户体验;2、需要展示公司logo、广告等信息。

二、Splash的三种实现

1、SplashActivity

将SplashActivity作为app启动的第一个activity,SplashActivity启动后延时一段时间跳转到MainActivity。网上可以搜到很多实现,这里就不展开了。

2、MainActivity中控制view的显示

将MainActivity作为app启动的第一个activity,MainActivity启动后先展示SplashView,延迟一段时间将SplashView隐藏并显示主页面。

3、设置Application或者MainActivity的主题

  • AndroidManifest.xml
<!-- 设置theme属性 -->
<application
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:name="com.xxx.MyApplication"
       android:theme="@style/my.NoTitleBar.Theme">
       
       <!-- 设置theme属性 -->
       <activity
           android:screenOrientation="portrait"
           android:name="com.xxx.views.MainActivity"
           android:theme="@style/my.NoTitleBar.Theme"
           android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
           android:windowSoftInputMode="adjustResize">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>

   </application>
  • res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="my.NoTitleBar.Theme" parent="@android:style/Theme.Light.NoTitleBar">
       <!-- android:windowBackground用drawable的layer-list标签实现,通用性比纯图片要好 -->
       <item name="android:windowBackground">@drawable/splash_bg</item>
   </style>
</resources>
  • res/drawable/splash_bg.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>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>

    <!--单独的slogan图片 并且设置下间距-->
    <item android:bottom="40dp">
        <bitmap
            android:gravity="bottom"
            android:src="@drawable/launch_splash" />
    </item>

</layer-list>

三、总结

前两种实现,在app初始化耗时的情况下,依旧会闪现白屏。而设置Application或者MainActivity的主题可以避免闪现白屏,但是无法设置延迟时间,适用于“启动阶段有耗时的初始化操作,影响到了用户体验”。若是需要使公司logo、广告等信息展示固定时间,则可以将设置主题和SplashActivity(或MainActivity中控制view的显示)结合使用来实现。

【参考文档】

  1. https://www.zhihu.com/question/29424875
  2. https://blog.csdn.net/qq_36455052/article/details/78429713
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值