0
1

More than 1 year has passed since last update.

SplashScreenについて(静止画編)

Last updated at Posted at 2023-01-15

こんにちはandroidでアプリ開発を学習中のみのむしと申します。
今回は、SplashScreenを学びましたので、実装方法を忘備録として残したいと思います。

SplashScreenを表示させるActivityを作成する

今回は、新規作成したActivityにSplashScreenを表示させ、その後にMainActivityにIntentする流れで作成していきます。

drawableに新規xmlファイルを作成する

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

<item>
    <bitmap android:src="@drawable/表示させたいファイル名" android:gravity="center"
        />
</item>

</layer-list>

themes.xmlファイルに以下内容に追記する

<!--SplashScreen-->
    
    <style name="SplashScreen" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <item name="android:windowBackground">@drawable/表示させたいファイル名</item>
        <item name="android:statusBarColor"  tools:targetApi="l" tools:ignore="ObsoleteSdkInt">?attr/colorPrimaryVariant</item>
    </style>

作成した新規Activity(SplashScreenMain)に以下内容を記述する

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreenMain extends AppCompatActivity {

    private final long TIME = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen_main);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

            //splashscreenを起動してから3秒後にMainActivityへ遷移する
                Intent intent = new Intent(SplashScreenMain.this,MainActivity.class);
                startActivity(intent);

            }
        },TIME);
    }
}

AndroidManifest.xmlファイルを編集する

デフォルトではMainActivityが最初に実行されるように設定されているため、SplashScreenMain
クラスが最初に実行されるよう以下内容に変更する

<activity
            android:name=".MainActivity"
            android:exported="true"/>
        <activity
            android:name=".SplashScreenMain"
            android:exported="true"
            android:theme="@style/SplashScreen"
            >
0
1
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
1