android启动页

作者: lxqljc | 来源:发表于2020-01-04 23:57 被阅读0次

android启动页

一、介绍

这里指的是点击桌面图标后,第一次启动后(冷启动)用户看到的第一个界面,通常冷启动后,此时用户界面还没渲染,看到的第一帧通常是黑屏或者白屏。为了让app的体验更好些,可以通过配置主题的方式去取代第一帧的背景。但是背景因设计稿的不同,需要作出不同的适配情况,比如:

  1. 一个居中logo + 背景 ,这种方式是最简单的,这种不用适配。
  2. 复杂元素的背景,这种需要适配多张图片,一般都是适配主流分辨率。
  3. 找出主流分辨率的安全区域,让设计元素在安全区域,这样可以很好的适配。

我个人是比较喜欢第一种,简单,也不会增大包的体积。所以下面以此为例。

二、实现步骤
  1. 配置启动页主题(values->styles.xml)

     <!-- 定义启动页主题-->
     <style name="LaunchScreen" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowFullscreen"/>
            <item name="android:windowBackground">@drawable/launch_background</item>
        </style>
    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
  2. 再新建一个values-v21文件夹,文件夹下新建文件styles.xml,主要处理有状态栏和导航栏的问题

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <!-- 定义启动页主题-->
        <style name="LaunchScreen" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowFullscreen" />
            <item name="android:windowBackground">@drawable/launch_background</item>
            <!--顶部和底部导航栏都不延伸,底部要有虚拟键,会挡住文字-->
            <item name="android:windowDrawsSystemBarBackgrounds">false</item>
        </style>
    </resources>
    
  3. 新建一个LaunchActivity页面

    package com.lxqljc.launchscreendemo
    
    import android.content.Context
    import android.content.Intent
    import android.os.Bundle
    import android.os.Handler
    import androidx.appcompat.app.AppCompatActivity
    
    /**
     * Author: lxqljc
     * Date: 2020-01-04
     * description: 描述 启动页面
     */
    class LaunchActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.launch_activity)
            startMainActivity(this)
        }
    
        /**
         * 跳转到首页
         */
        private fun startMainActivity(context: Context) {
            val handler = Handler()
            handler.postDelayed({
                val intent = Intent(context, MainActivity::class.java)
                context.startActivity(intent)
                finish()
            }, 2 * 1000)
        }
    
    }
    
  4. 最后在AndroidMainfest.xml中配置启动页,并且配置启动页主题。

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.lxqljc.launchscreendemo">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
            tools:ignore="GoogleAppIndexingWarning">
    
            <activity
                android:name=".LaunchActivity"
                android:screenOrientation="portrait"
                android:theme="@style/LaunchScreen">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".MainActivity"
                android:screenOrientation="portrait" />
        </application>
    
    </manifest>
    
三、效果图
image.png

相关文章

网友评论

    本文标题:android启动页

    本文链接:https://www.haomeiwen.com/subject/fifqactx.html