0
0

[android]アプリ起動時にスプラッシュ画面を挟む

Posted at

LINEなどのアプリを起動した際に、初めに数秒表示されるスプラッシュ画面。
今回はそちらを実装してみたいと思います。
スプラッシュ画面を導入するメリットは以下になります。

  • ユーザーエクスペリエンスの向上
  • アプリ読み込み待ちの不安感軽減

2つ目のメリットが結構大きいと思います。

前提条件

開発環境:Android Studio Dolphin | 2021.3.1 Patch 1
言語:Java
テンプレート:任意(本記事ではEmpty Activityを使用)

実装

スプラッシュ画面の導入方法はいくつかありますが、
今回はスプラッシュ画面用のActivityを作成する方法で実装していきます。

スプラッシュ画面で使用するアイコン作成

File > New > Vector Asset から作成できます。
デフォルトでresフォルダのdrawableに格納されます。

image.png

スプラッシュ画面の作成

res フォルダのlayoutの中に、スプラッシュ画面用のxmlファイルを作成します。
レイアウトエディタでImageViewを用いて作成すると簡単です。

activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="77dp"
        android:layout_height="68dp"
        app:srcCompat="@drawable/ic_android_black_24dp"
        tools:layout_editor_absoluteX="167dp"
        tools:layout_editor_absoluteY="331dp"
        android:contentDescription="TODO"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

SplashActivityの作成

File > New > Java Class からSplashActivityを作成

SplashActivity.java
package com.example.sampleapplication;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        setContentView(R.layout.activity_splash);

        Thread timer = new Thread() {
            public void run()
            {
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                } finally {
                    Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                }
            }
        };
        timer.start();
    }
}

1秒間sleepを用いてスプラッシュ画面を表示させます。

AndroidManifestの修正

最後にAndroidManifestを修正します。
MainActivityを呼び出す前に、SplashActivityを呼び出すようにしてあげます。

AndroidManifest.xml
 <application ...>
        <activity
            android:name=".SplashActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

エミュレータ起動

device-2024-02-27-025306.gif

最後に

スプラッシュ画面用のActivityを作成する以外にも、
Activityのテーマを変更する方法がありますが、スプラッシュ画面用のActivityを作成した方が、自由度の高いスプラッシュ画面を作成することができると思います。
Android 12 以降では、SplashScreen APIを用いたスプラッシュ画面表示が推奨されているので、こちらも使えるようにならないとですね。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0