Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีสร้าง Splash Screen ในแอป Android

ตัวอย่างนี้สาธิตวิธีสร้าง Splash Screen (หน้าจอโหลด) ใน Android อย่างถูกต้อง โดยใช้ Theme แทนการใช้ Thread.sleep()

⚠️ หมายเหตุสำคัญ

โค้ดตัวอย่างเดิมใช้ Thread.sleep(3000) ใน Main Thread - นี่เป็นวิธีที่ผิด!

จะทำให้ UI ค้าง (ANR) และไม่ได้เป็น Splash Screen จริง วิธีที่ถูกต้องคือใช้ Theme + Splash Activity แยก

วิธีที่ถูกต้อง: ใช้ Theme Splash Screen (แนะนำ)

ขั้นตอนที่ 1: สร้าง Drawable สำหรับพื้นหลัง Splash (res/drawable/splash_background.xml)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
    <item>
        <color android:color="@color/splash_background_color"/>
    </item>
    <item>
        <bitmap
            android:src="@drawable/ic_logo"
            android:tileMode="disabled"
            android:gravity="center"/>
    </item>
</layer-list>

ขั้นตอนที่ 2: กำหนดสี (res/values/colors.xml)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="splash_background_color">#5456e1</color>
</resources>

ขั้นตอนที่ 3: สร้าง Theme (res/values/styles.xml)

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>
</resources>

ขั้นตอนที่ 4: SplashActivity.java

package com.example.sample;

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

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ไม่ต้อง setContentView() เพราะใช้ Theme background
        
        // หน่วงเวลา 2-3 วินาที หรือโหลดข้อมูลเริ่มต้นที่นี่
        new android.os.Handler().postDelayed(() -> {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            finish(); // ปิด Splash Activity
        }, 2500); // 2.5 วินาที
    }
}

ขั้นตอนที่ 5: MainActivity.java (หน้าหลัก)

package com.example.sample;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ขั้นตอนที่ 6: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.sample">

    <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">

        <!-- Splash Activity เป็น Launcher ใช้ SplashTheme -->
        <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>

        <!-- Main Activity -->
        <activity android:name=".MainActivity" />

    </application>
</manifest>

ข้อดีของวิธีนี้

  • ✅ ไม่บล็อก Main Thread (No ANR)
  • ✅ แสดง Splash ทันทีที่เปิดแอป (Instant)
  • ✅ รองรับ Cold Start ได้ดีที่สุด
  • ✅ โค้ดสะอาด แยกหน้าที่ชัดเจน

Android 12+ (API 31+) - Splash Screen API

Android 12 มี Splash Screen API เบื้องต้น ให้ใช้ androidx.core:core-splashscreen แทน

// build.gradle
implementation "androidx.core:core-splashscreen:1.0.1"

// SplashActivity.java (Android 12+)
@Override
protected void onCreate(Bundle savedInstanceState) {
    SplashScreen.installSplashScreen(this);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    // ...
}

การทดสอบ

รันแอป → จะเห็น Splash Screen แสดงโลโก้พร้อมพื้นหลังสี → หลัง 2.5 วินาทีเข้าสู่ MainActivity