Android启动画面
时间:2020-02-23 14:29:17 来源:igfitidea点击:
Android启动画面是启动应用程序时用户可见的第一个屏幕。
闪屏是应用程序中最重要的屏幕之一,因为它是用户对应用程序的首次体验。
启动屏幕用于显示某些动画(通常是应用程序徽标)和插图,同时获取下一个屏幕的一些数据。
Android启动画面
通常,在AndroidManifest.xml文件中设置了以下意图过滤器的Activity是Splash Activity。
<intent-filter>
<action android:name="android.intent.action.MAIN"
<category android:name="android.intent.category.LAUNCHER"
</intent-filter>
Android启动画面示例项目结构
创建初始屏幕的方法很少,即应用程序的启动屏幕。
让我们看看它们中的每一个。
闪屏经典方法
由于应用程序需要时间来加载Splash Activity的布局文件,因此出现冷启动。
因此,我们将使用应用程序主题的功能来创建初始布局,而不是创建布局。
在创建布局之前,将实例化应用程序主题。
我们将在" android:windowBackground"属性内设置一个可绘制对象,该对象将包含活动的背景和使用图层列表的图标,如下所示。
splash_background.xml
package com.theitroad.splashscreen;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//This method will be executed once the timer is over
Intent i = new Intent(SplashActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, 5000);
}
}
我们将以下样式设置为活动的主题。
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context="com.theitroad.splashscreen.SplashActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="72dp"
android:layout_height="72dp"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@id/imageView"
</android.support.constraint.ConstraintLayout>
SplashActivity.java文件应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/black"
<item>
<bitmap
android:gravity="center"
android:src="@mipmap/ic_launcher"
</item>
</layer-list>
注意:活动的主题设置在其他任何主题之前。
因此,以上方法将使我们的应用程序更快速地启动。
使用主题并从SplashActivity中删除布局是创建初始屏幕的正确方法。

