Android

[Android] 안드로이드 SplashScreen

pyflu 2024. 4. 1. 20:21

Android 12부터 SplashScreen API를 사용하면 실행 시 앱 내 모션, 앱 아이콘을 보여주는 스플래시 화면, 앱 자체로의 전환을 비롯한 애니메이션과 함께 앱을 실행할 수 있습니다. 이 글에서는 코드를 통해 SplashScreen을 구현해보겠습니다.


안드로이드 SplashScreen
안드로이드 SplashScreen

 

 

1. build.gradle (Module:app)

- 자체 제공하는 API를 등록한 후 Sync Now를 눌러 반영해줍니다.

SplashScreen

dependencies {
    
    ...

    implementation("androidx.core:core-splashscreen:1.0.1")
}

 

 

2. themes.xml

- themes.xml 파일에 style을 만들어 Theme.SplashScreen을 상속받은 후 원하는 값을 입력해줍니다.

SplashScreen

<resources xmlns:tools="http://schemas.android.com/tools">
    
    ...

    <!-- SplashScreen style 작업 / parent에 "Theme.SplashScreen을 상속 받아야한다. -->
    <style name="AppTheme.SplashScreen" parent="Theme.SplashScreen">

        <!-- SplashScreen 이미지 설정 (색상 / 이미지) -->
        <!-- 단말기에 따라 이미지가 깨질 수 있으니 vector drawable 추천 -->
        <item name="windowSplashScreenAnimatedIcon">@drawable/profile</item>

        <!-- 이미지의 지속 시간 설정 (최대 1000ms) -->
        <!-- 이를 설정해도 스플래시 화면이 표시되는 실제 시간에는 영향을 미치지 않음... -->
        <item name="windowSplashScreenAnimationDuration">1000</item>

        <!-- SplashScreen 배경색 설정 (색상 / 이미지) -->
        <item name="windowSplashScreenBackground">@color/black</item>

        <!-- Splash Screen을 보여준 후 다음에 나올 화면의 테마를 설정해준다. (필수) -->
        <item name="postSplashScreenTheme">@style/Theme.Android_Splash_Project</item>

        <!-- SplashScreen 하단에 표시할 브랜딩 이미지를 설정 (권장 X) -->
        <!-- <item name="android:windowSplashScreenBrandingImage" tools:targetApi="S">@drawable/ic_launcher_foreground</item> -->
    </style>
    
</resources>

 

 

3. AndroidManifest.xml

- AndroidManifest.xml 파일로 와서 MainActivity의 theme 속성에 아까 작성한 style을 등록해줍니다.

SplashScreen

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Android_Splash_Project"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/AppTheme.SplashScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

 

4. MainActivity.kt

- MainActivity.kt 파일로 와서 setContentView전에 installSplashScreen()을 입력해주면 됩니다.

package kr.co.lion.android_splash_project

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // SplashScreen 적용 (setContentView 이전에 작성)
        installSplashScreen()

        setContentView(R.layout.activity_main)
    }
}
728x90

'Android' 카테고리의 다른 글

[Android] 안드로이드 textView 클릭 효과 주기  (30) 2024.02.25