Kotlin

[Kotlin] Splash Screen 만들기

eulBlue 2023. 8. 9. 01:39

📱테스트 환경

Samsung Galaxy Android 13 • Android 10


😢 내가 겪은 문제

코틀린 개발을 강의 하나보고 구글에 검색해가면서 처음 개발하다보니 .. 이런게 있는지도 잘 몰랐다 ..

내가 만든건 내가 생각해도 좀 이상한것같다 .. 근데 잘 되긴한다 ....!! 그냥 내가 참고하고 기록하려고 하는거니 .. 이해가 필요하다 ..

딜레이를 1초 준 뒤 MainActivity 로 이동했다. ( 2 ~ 3 초도 적용해봤는데 , 나는 1초가 가장 이쁜것 같았다. )

SplashActivity.kt

class SplashActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar?.hide()
        setContentView(R.layout.activity_splash)
        // Create a Handler
        val handler = Handler()

        val delayMillis = 1000L

        handler.postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
            finish()
        }, delayMillis)
    }
}

Manifest 에서 ShlashActivity 를 LAUNCHER Activity 로 설정해줬다.

그리고 theme 를 설정해줬는데 해당 코드를 보면 충분히 이해할 수 있다

AndroidManifest.xml

<activity
    android:name=".SplashActivity"
    android:exported="true"
    android:theme="@style/SplashTheme"
    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

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

windowIsTranslucenttrue 로 설정해준 이유는 이걸 안해주니까 앱이 실행될때 흰화면 ( 또는 검정화면 ) 으로 시작하더라 ...

style.xml

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
</style>

그 이후에는 그냥 간단하게 화면 꽉차게 스플레시 이미지를 적용해줬다 !!

activity_splash.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/images_splash"/>
</LinearLayout>

이렇게 하고 나면 ?

1. 앱을 실행시킬 때 SplashActivity 로 처음 들어와서 화면을 보여주고

2. MainActivity 로 이동하게 된다.

근데 이렇게 안하고 다른 블로그들 보면 build.gradle 에 implementation 이용해서 쉽고 간편하게 하는 방법들이 아주 잘 설명되어있다.

아쉽지만 나는 이해가 잘 안돼서 이렇게 했는데 , 자기에게 맞는 방법으로 만들면 될 것 같다 !!

https://developer.android.com/develop/ui/views/launch/splash-screen

 

스플래시 화면  |  Android 개발자  |  Android Developers

이 페이지는 Cloud Translation API를 통해 번역되었습니다. Switch to English 스플래시 화면 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. 중요: Android 11 이하에서

developer.android.com