当前位置:首页 > 安卓源码 > 技术博客 >

在Android上创建启动画面的正确方法

时间:2018-12-15 21:09 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

众所周知,启动画面是用户对您的应用程序的第一次体验。 它通常用于在完全应用程序设置之前显示某种进度。 根据Google材料设计规范,Splash Screen遵循称为Launch Screen的模式。 您可以在 此处 找到规范 。 常见的错误 在大多数应用程序开发人员中,使用启

众所周知,启动画面是用户对您的应用程序的第一次体验。它通常用于在完全应用程序设置之前显示某种进度。根据Google材料设计规范,Splash Screen遵循称为Launch Screen的模式。您可以在此处找到规范

常见的错误

在大多数应用程序开发人员中,使用启动画面来展示品牌图标或图片几秒钟。这是大多数开发人员遵循的常见做法。使用浪费用户时间的闪屏不是一个好主意。这应该严格避免。

使用常见方法,您可能还会导致在启动启动期间出现空白页面问题

正确的方法

实现启动画面的正确方法略有不同。在新方法中,将启动画面的背景指定为活动的主题背景。

空白页面问题的根本原因是,只有在完全初始化应用程序后,您的布局文件才可见。

不要为启动活动创建布局文件。而是将活动的主题背景指定为初始布局。

履行

要实现上述方法,首先res / drawable文件夹中创建一个XML drawable splash_background.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>

在下一步中,将splash_background.xml设置为主题中的splash活动的背景。为您的启动活动添加新的SplashTheme


<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
</resources>

SplashTheme配置AndroidManifest.xml中的 splash活动主题


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.androidjavapoint.splashscreen">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>
        <activity android:name=".HomeActivity" />
    </application>
</manifest>

为Splash创建一个没有XML布局文件的空活动。该课程将简单地重定向到家庭活动。


package com.androidjavapoint.splashscreen;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Start home activity
        startActivity(new Intent(SplashActivity.this, HomeActivity.class));
        // close splash activity
        finish();
    }
}

请注意,我们没有此SplashActivity的setContentView()视图从主题显示,这种方式比创建布局更快。

如果您查看启动屏幕显示的时间与应用程序配置自身所花费的时间完全相同,因为冷启动(首次启动)。如果应用程序已缓存,则启动屏幕几乎会立即消失。

希望使用本文,您将能够以正确的方式使您的启动画面工作。

在Android上创建启动画面的正确方法 转载https://www.codesocang.com/appboke/39141.html

技术博客阅读排行

最新文章