View

Splash 화면

제롱구리 2024. 5. 22. 19:13
728x90

금일 알아 볼 것

오늘은 오랫만에 글을 포스트하려고 한다.
한 동안 프로젝트 등에 찌들어서 쓸 시간이 없었다.
오늘 알아 볼 것은 Splash 화면이다.


Splash 화면

Splash 화면은 앱을 실행하게 되면 가장 먼저 잠깐 뜨는 화면을 말한다.
오늘 글을 포스트하게 된 이유는 해당 Splash화면을 구현하는데 있어서 안드로이드 12부터는 고유 Splash 화면을 지원해주기 때문이다.
이게 좋기는 하지만 뭔가 아직 자유로운 화면을 짜기는 무리가 있어 보인다.

그래서 총 3가지 방벙이 있다.

  1. Android12 SplashScreen 기능을 사용하는 방법

    • 해당 방법은 Android 12에서 지원해주는 기능을 사용하는 방법이다.
  2. SplashScreen API 사용하는 방법

    • 해당 방법은 기존 Android 12 이하의 버전에도 Android 12의 SplashScreen과 같은 형식의 기능을 제공하도록 도와주는 라이브러리이다.
  3. Android12의 SplashScreen의 아이콘을 투명화하고 기존의 Splash 보여주기

    • 해당 방법은 옛날 방법인 Activity화면으로 Splash화면을 보여주기 위해서 위에 있는 기능을 투명화하는 방법이다.

이렇게 총 3가지 방법이 있다.
1,2번의 경우는 따로 Splash를 구현한 xml 파일이 필요가 없다. 즉 themes에서 설정해서 바로 띄워주는 것이다.
3번의 경우는 내 맛대로 Splash를 커스텀할 때 사용하는 방법이다.


Android 12 SplashScreen 기능

SplashScreen의 Theme 속성을 알아보자.

Android 12 SplashScreen 기능의 속성


<!-- Splash 화면의 백그라운드 컬러 지정 -->
<item name="android:windowSplashScreenBackground">@color/color</item>

<!-- Splash 화면의 중앙에 위치할 아이콘 Drawable 지정 -->
<item name="android:windowSplashScreenAnimatedIcon">@drawable/icon</item>

<!-- Splash 화면 중앙의 아이콘 주변 Background Color 지정 -->
<item name="android:windowSplashScreenIconBackgroundColor">@color/color</item>

<!-- Splash 화면이 나타나는 시간 지정 -->
<item name="android:windowSplashScreenAnimationDuration">duration_int</item>

<!-- Splash 화면 하단에 나타낼 브랜딩이미지 지정 (비권장) -->
<item name="android:windowSplashScreenBrandingImage">@drawable/branding_image</item>

themes.xml에서 는 아래와 같이 사용하면된다.


<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="Theme.SplashEx" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ....
        <item name="android:windowSplashScreenBackground" tools:targetApi="S">@color/color</item>
        <item name="android:windowSplashScreenAnimatedIcon" tools:targetApi="S">@drawable/icon</item>
        <item name="android:windowSplashScreenIconBackgroundColor" tools:targetApi="S">@color/color</item>
        <item name="android:windowSplashScreenAnimationDuration" tools:targetApi="S">duration_int</item>
        <item name="android:windowSplashScreenBrandingImage" tools:targetApi="S">@drawable/branding_image</item>
    </style>
</resources>


SplashScreen API 사용하는 방법

일단 아래와 같이 dependency를 추가해준다.

build.gradle(Module: app)

dependencies {

    ...

    //splashscreen api
    implementation 'androidx.core:core-splashscreen:1.0.0-rc01'
}


themes.xml 파일적용

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
    <item name="windowSplashScreenBackground">@color/purple_200</item>
    <item name="windowSplashScreenAnimatedIcon">@drawable/ic_jd</item>
    <item name="postSplashScreenTheme">@style/Theme.SplashEx</item>
</style>

themes.xml에서 새로운 style을 생성합니다. 이 때 style의 이름은 Thme.App.Starting이여야 합니다.

위에서 사용하였던 속성과 SplashScreen Api의 속성은 유사합니다. 다른 점은 api는 windowSplashScreenBrandingImage를 사용할 수 없고 postSplashScreenTheme이 추가되었습니다.

postSplashScreenTheme은 Splash가 끝난 후 사용될 Theme을 지정해주는 것입니다. 내 App의 기본 Theme으로 연결시켜 줍니다.



AndroidManifest.xml

<application
    ...
    android:theme="@style/Theme.SplashEx">
    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:theme="@style/Theme.App.Starting">
        <intent-filter>
            ...
        </intent-filter>
    </activity>
</application>

위에 코드 처럼 Manifest에서 application 단에서 기본 테마를 작성해주고
Splash가 끝난 뒤 바로 나올 화면에 theme를 아까 설정해준 Theme.App.Starting를 설정해 주는 것이다.
(간단히 말해서 해당 SplashScreen API는 theme에서 Splash 화면을 설정해주고 바로 구현해 주는 것이다.
그러므로 Splash를 보여주는 xml이 필요없고 특정 화면 이전에 Splash가 뜨게끔하기 위해서 위와 같이 설정해주는 것이다.)

application의 theme에는 기본 Theme을 지정해주고 MainActivity의 theme에는 아까 만든 Theme.App.Starting을 지정해줍니다.



MainActivity.kt


class MainActivity : AppCompatActivity() {

    private lateinit var splashScreen: SplashScreen

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // setContentView하기 전에 installSplashScreen() 필수
        installSplashScreen()
        setContentView(R.layout.activity_main)
    }
}

마지막으로 Splash를 띄워줄 화면에 가서 setContentView 이전에 installSplashScreen()함수를 호출 해주면된다.


실제 적용사례












여기서 한 가지 더 해야될 부분이 있다.
만약에 Activity 형식으로 Splash를 구현하려고 하면 기존의 Splash API를 사용하고 막아야되는(안그러면 12에서 자체 지원하는 것 때문에 문제가 생김) 아에 막는 것은 불가능 한것으로 보인다.
투명하게 해도...흰색이 화면이 잠깐 떳다가 사라지기 때문이다. 그래서 차라리 Splash 화면에 설정하고자하는 화면의 배경 색을 같게 주고 icon을 투명하게 하거나 덮어 버린는게 최선의 방법 같다...

'TIL > 스터디' 카테고리의 다른 글

.gitignore  (0) 2024.04.19
앱 배포시 apk -> aab로 왜 바꿀까?  (0) 2024.04.15
함수 참조 (RecyclerView) 사용  (0) 2024.04.11
코틀린 함수 참조  (0) 2024.03.29
TabLayout 스크롤 감지 (Viewpager안썻을 때)  (0) 2024.03.28
Share Link
reply
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30