[Android/Kotlin] Splash ํ™”๋ฉด ๋งŒ๋“ค๊ธฐ

2020. 12. 31. 22:11ใ†๐Ÿ“ฑAndroid/๐Ÿ“ Android Study

๋ฐ˜์‘ํ˜•

 

Splash ํ™”๋ฉด์€ ์•ฑ ์‹œ์ž‘ํ•˜์ž๋งˆ ์ž ์‹œ๋™์•ˆ ๋œจ๋Š” ํ™”๋ฉด์ธ๋ฐ์š”! ์˜ค๋Š˜์€ ์ •๋ง ๊ฐ„๋‹จํ•œ Splash ํ™”๋ฉด์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ„๋‹จํ•˜๊ฒŒ ์ ์–ด๋ณด๋ คํ•ฉ๋‹ˆ๋‹ค!

 

 

๐Ÿ“Œ SplashActivity ๋งŒ๋“ค์–ด์ฃผ๊ธฐ

Splash ํ™”๋ฉด์œผ๋กœ ์‚ฌ์šฉํ•  Activityํ•˜๋‚˜๋ฅผ ์ƒ์„ฑํ•ด์ฃผ์„ธ์š”.

 

๐Ÿ“Œactivity_splash.xml ํ™”๋ฉด ๊ตฌ์„ฑํ•˜๊ธฐ

์›ํ•˜๋Š”๋Œ€๋กœ xml ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•ด์ฃผ์„ธ์š”. ์ €๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ธฐ๋ณธ android ImageView๋งŒ ๋„์› ์Šต๋‹ˆ๋‹ค.

<?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"
    android:background="@color/colorPrimary"
    tools:context=".SplashActivity">
    
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:src="@drawable/ic_launcher_foreground"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

๐Ÿ“Œ SplashActivity.kt Handler ๊ตฌํ˜„ํ•ด์ฃผ๊ธฐ

SplashActivity.kt์— Handler๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. Hadler์˜ ๋ชฉ์ ์€ ํŠน์ • ์‹œ๊ฐ„๋™์•ˆ SplashActivity ํ™”๋ฉด์„ ๋ณด์—ฌ์ฃผ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค

์ €๋Š” Splash ํ™”๋ฉด์„ 3์ดˆ๋™์•ˆ ๋ณด์—ฌ์ค€ ๋’ค MainActivitiy๋กœ ์ด๋™ํ•˜๊ฒŒ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

Splash ํ™”๋ฉด์€ ์•ฑ ์ฒ˜์Œ ์‹œ์ž‘์— ๋”ฑ 1๋ฒˆ๋งŒ ๋ณด์—ฌ์ง€๋Š” ํ™”๋ฉด์ด๋ฏ€๋กœ ๊ผญ ํ™”๋ฉด์ „ํ™˜ํ›„ finish() ํ•ด์ฃผ์…”์•ผํ•ฉ๋‹ˆ๋‹ค!

  Handler().postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            startActivity(intent)
            finish()
        },DURATION)
        
        
 companion object {
        private const val DURATION : Long = 3000
    }

 

SplashActivity.kt ์ „์ฒด ์ฝ”๋“œ

package com.example.splash

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

class SplashActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)

        Handler().postDelayed({
            val intent = Intent(this, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
            startActivity(intent)
            finish()
        },DURATION)

    }
    companion object {
        private const val DURATION : Long = 3000
    }

    override fun onBackPressed() {
        super.onBackPressed()
    }
}

 

๐Ÿ“Œ SpalshActivity ์ธํ…ํŠธ ํ•„ํ„ฐ ์„ค์ •ํ•ด์ฃผ๊ธฐ

๋งจ ์ฒ˜์Œ์— MainActivity๊ฐ€ ์•„๋‹Œ SplashActivity๊ฐ€ ์ œ์ผ ๋จผ์ € ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ ์ธํ…ํŠธ ํ•„ํ„ฐ๋ฅผ SplashActivity๋กœ ์ง€์ •ํ•ด์ค๋‹ˆ๋‹ค.

MainActivity์— ์กด์žฌํ•˜๋Š” ์ธํ…ํŠธ ํ•„ํ„ฐ๋ฅผ SplashActivity๋กœ ์˜ฎ๊ฒจ์ค๋‹ˆ๋‹ค.

 

AndroidManifest.xml

 <activity android:name=".SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

 

๐Ÿ“Œ Splash ํ™”๋ฉด NoActionBar๋กœ ์„ค์ •ํ•ด์ฃผ๊ธฐ

๋ณดํ†ต์˜ Splashํ™”๋ฉด์—์„œ๋Š” ActionBar๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— NoActionBar Theme๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

splash ํ™”๋ฉด์„ ์œ„ํ•œ NoActionBar๋กœ ์ด๋ฃจ์–ด์ง„ Splash style์„ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

Action Bar

 

style.xml

<style name="Splash" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

AndroidManifest.xml

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

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

 

 

์ด๋ ‡๊ฒŒ Splash ํ™”๋ฉด ๊ตฌํ˜„์ด ๋์ด ๋‚ฌ์Šต๋‹ˆ๋‹ค! ์•ฑ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

Splash ํ™”๋ฉด์„ ์ด์šฉํ•˜์—ฌ ์ข€ ๋” ์•ฑ์˜ ์™„์„ฑ๋„๋ฅผ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค : ) ์›ํ•˜๋Š” ํ™”๋ฉด์œผ๋กœ Splash ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•ด๋ณด๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

๋ฐ˜์‘ํ˜•