Android:启动页--最佳实践

2019-08-23  本文已影响0人  Mr_panmin

一、前言

Android 开发过程中启动页是必不可少的,但是我们经常会看到启动打开后是先白屏或者黑屏,然后才会显示出启动页的图片,本文会解析此现象的原因,以及给出解决方案

1.1 启动白屏或黑屏的原因

AndroidManifest.xml中的application标签中设置了theme,当设置的theme是Light类型时启动白屏,当设置theme是Dark类型时启动黑屏

1.2 进一步分析

当系统启动一个app时,zygote进程会fork一个app子进程,进程创建后在启动activity时就会创建一个window,这个window会使用theme中设置的windowBackground来显示背景颜色或者图片,当使用LightDark时跟进代码就能看到默认设置的windowBackground就是白色和黑色。

二、启动页--常规做法

2.1 创建Activity:SplashActivity

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        Handler().postDelayed({
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        },3000)
    }
}

2.2 给启动页创建background:splash_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap android:gravity="center"
                android:src="@drawable/splash_screen"/>
    </item>
</layer-list>

下面重点说下bitmap的属性功能:

2.3 给启动页创建style:SplashTheme

<style name="SplashTheme" parent="AppTheme">
  <item name="windowNoTitle">true</item><!--无标题-->
  <item name="android:windowFullscreen">true</item><!--全屏-->
  <item name="android:windowIsTranslucent">true</item><!--半透明-->
  <item name="android:windowBackground">@drawable/splash_background</item>
</style>

2.4 在AndroidManifest.xml中给SplashActivity设置style

<activity android:name=".SplashActivity"
          android:theme="@style/SplashTheme">
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>

    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity>

通过上面的4步设置就能够解决打开app白屏或黑屏的问题了。

三、最佳实践

3.1 场景1:只有屏幕中间有一张图片或者是图片+文字

通过上面的方式就能实现,注意bitmap使用gravitycenter让图片和文字居中显示

3.2 场景2:屏幕中部和底部都有图片或者文字

这种上下布局的,例如微博,可以把图片切成上下两个,或者切割图片上下左右使图片整体能够居中显示

方式一:将图片切成屏幕中央显示的图片和底部图片

splash_background.xml中就需要换种写法了

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 整体的背景颜色 -->
    <item>
        <color android:color="@color/white"/>
    </item>
    <!-- 顶部 -->
    <item>
        <bitmap android:gravity="center"
                android:src="@drawable/splash_center"/>
    </item>
    <!-- 底部 -->
    <item>
        <bitmap android:gravity="bottom|center_horizontal"
                android:src="@drawable/splash_bottom"/>
    </item>
</layer-list>

当手机底部虚拟导航栏时,底部图片会被遮挡,使导航栏透明即可:

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        Handler().postDelayed({
            startActivity(Intent(this,MainActivity::class.java))
            finish()
        },3000)
    }
}

这句window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)是关键

方式二:将图片设置成background而不是windowBackground

<style name="SplashTheme" parent="AppTheme">
  <item name="windowNoTitle">true</item>
  <item name="android:windowFullscreen">true</item>
  <item name="android:windowIsTranslucent">true</item>
  <item name="android:windowBackground">@android:color/white</item>
  <item name="android:background">@drawable/splash_background</item>
</style>

用background来设置背景图片

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/white"/>
    <item>
        <bitmap android:gravity="clip_vertical|clip_horizontal"
                android:src="@drawable/splash_screen"/>
    </item>
</layer-list>

利用bitmap的gravity属性,用clip_vertical|clip_horizontal来裁剪图片

注意:利用第二种方式时不能使用window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION),这个会导致图片被拉伸

上一篇 下一篇

猜你喜欢

热点阅读