최데브는 오늘도 프로그래밍을 한다.

splahScreen 안드로이드 12 버전에 적용하기 본문

Android/Android UI

splahScreen 안드로이드 12 버전에 적용하기

최데브 2023. 1. 14. 14:59
반응형

최근 Dev6 프로젝트를 새로 개발하면 스플래시 스크린을 만들일이 생겼다.

이번에 스플래시 스크린을 만들면서 안드로이드 12 부터 바뀐 스플래시 스크린을 적용해보며 정리하는 글이다.

 

 

먼저 라이브러리를 추가하자.

dependencies {
    implementation 'androidx.core:core-splashscreen:1.0.0-beta01'
}

위 처럼 스플래시 스크린 라이브러리를 import 해준다.

이 라이브러리를 사용하려면 minSdk 는 21이상 , targetSdk 는 31 이니 참고하자.

 

themes.xml 에 스플래시 스크린 테마를 추가하자.

    <style name="AppTheme.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/white</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/back_arrow</item>
        <item name="android:windowSplashScreenBrandingImage" tools:targetApi="s">@drawable/back_arrow</item>
        <item name="windowSplashScreenAnimationDuration">1000</item>
        <item name="postSplashScreenTheme">@style/Theme.rejord.designsystem</item>
        <item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
        <item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
        <item name="android:navigationBarColor">@color/white</item>
    </style>

이렇게만 보여주면 뭐가 무슨 옵션인지 찾아보기 귀찮으니 몇개 적어보자.

  • windowSplashScreenBackground: splashscreen의 배경
  • windowSplashScreenAnimatedIcon: splashscreen에 표시할 이미지
  • windowSplashScreenAnimationDuration: splashscreen이 표시되는 시간으로 최대 1,000ms 
  • postSplashScreenTheme: splashscreen 처리가 끝나고 전환될 Theme
  • windowSplashScreenBrandingImage : 스플래시 화면 하단에 보여지는 브랜드 이미지

postSplashScreenTheme 는 스플래시 화면이 끝나고 처음 나오는 Activity 에서 사용하는 테마를 적어주면 된다.

예를 들어 스플래시 화면이 끝나고 MainActivity 를 띄운다면 거기서 사용하는 테마를 적어주면 된다.

스플래시 테마를 보여주고 자연스럽게 액티비티 테마를 보여주기 위함이다.

 

나머지 아이콘이나 배경 같은건 원하는대로 하면 된다.

 

AndroidManifest.xml에 설정 추가하기

그냥 위에 처럼 적어주기만 한다고 스플래시가 되는건 아니다.

스플래시가 사라지면 보여질 Activity 에 테마를 바꿔줘야한다.

나의 경우는 스플래시 화면이 꺼지면 MainActivity 를 보여주려고 하기 때문에 

  <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:exported="true"
            android:theme="@style/AppTheme.Starting">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

이렇게 적어줬다 theme 쪽만 보면된다. 아까 만들어준 스플래시 테마를 여기에 적어준다.

 

액티비티에서 스플래시 화면 로딩하기

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()

        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

    }
}

installSplashScreen() 를 적어주면 스플래시 화면이 잘 불리는걸 확인할 수 있다.

 

짜잔 스플래시 화면

 

내가 겪은 오류

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 

라는 오류가 자꾸 나와서 스트레스였는데 스플래시 테마를 위한 themes.xml 을 다른 곳에 따로 만들어줘서 생긴 문제였다.

 

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.rejord.designsystem" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorAccent">@color/mainColor</item>
        <item name="colorPrimary">@color/mainColor</item>
    </style>

    <style name="AppTheme.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/white</item>
        <item name="windowSplashScreenAnimatedIcon">@drawable/back_arrow</item>
        <item name="android:windowSplashScreenBrandingImage" tools:targetApi="s">@drawable/back_arrow</item>
        <item name="windowSplashScreenAnimationDuration">1000</item>
        <item name="postSplashScreenTheme">@style/Theme.rejord.designsystem</item>
        <item name="android:windowLightStatusBar" tools:targetApi="23">true</item>
        <item name="android:windowLightNavigationBar" tools:targetApi="o_mr1">true</item>
        <item name="android:navigationBarColor">@color/white</item>
    </style>
</resources>

이렇게 기존에 사용하는 테마와 같은 곳에 만들어주니 짜잔하고 해결됐다.

반응형
Comments