Dev/Android

JetpackCompose - SplashScreen API

healthyryu 2023. 11. 16. 19:14

과업 정의 :

- 스플래시 스크린은 3초 유지하면서 로고는 애니메이션 처리 한다.

 

1. 라이브러리 추가

implementation("androidx.core:core-splashscreen:1.1.0-alpha02")

 

2. 컴파일 버전 맞추기

android {
    compileSdk = 34
    ...
}

 

Dependency 'androidx.core:core-splashscreen:1.1.0-alpha02' requires libraries and applications that
      depend on it to compile against version 34 or later of the
      Android APIs.

 

 

 

3. Splash 화면을 위한 Theme 에서 스타일 적용

기본 themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.AnimatedScreenSplash" parent="android:Theme.Material.Light.NoActionBar" />

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="windowSplashScreenBackground">@color/black</item>
        <item name="postSplashScreenTheme">@style/Theme.AnimatedScreenSplash</item>
    </style>
</resources>

 

android 31 버전의 themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <item name="android:windowSplashScreenBackground">@color/black</item>
        <item name="postSplashScreenTheme">@style/Theme.AnimatedScreenSplash</item>
    </style>

</resources>

 

4. Manifest 에서 Theme 설정 변경

<?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:theme="@style/Theme.App.Starting"
        tools:targetApi="31">
        <activity
            ...
            android:theme="@style/Theme.App.Starting">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

 

 

5. Splash 화면이 3초 유지하고 메인화면으로 넘어가기 위한 ViewModel 처리

class MainViewModel : ViewModel() {

    private val _isReady = MutableStateFlow(false)
    val isReady = _isReady.asStateFlow()

    init {
        viewModelScope.launch {
            delay(3000L)
            _isReady.value = true
        }
    }
}

 

6. 앱이 시작하는 Activity (MainActivity) 에서 SplashScreen 화면 처리

class MainActivity : ComponentActivity() {

    private val viewModel by viewModels<MainViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        installSplashScreen().apply {
            setKeepOnScreenCondition {
                !viewModel.isReady.value
            }
        }
        
        ...
    }
}

 

 

7. 애니메이션 처리

- 적용할 애니메이션 작업 ( logo_animator.xml )

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:interpolator="@anim/overshoot_interpolator">

    <propertyValuesHolder
        android:propertyName="rotation"
        android:valueFrom="0.0"
        android:valueTo="360.0"
        android:valueType="floatType" />
    <propertyValuesHolder
        android:propertyName="scaleX"
        android:valueFrom="0.0"
        android:valueTo="0.4"
        android:valueType="floatType" />
    <propertyValuesHolder
        android:propertyName="scaleY"
        android:valueFrom="0.0"
        android:valueTo="0.4"
        android:valueType="floatType" />
</objectAnimator>

 

- 애니메이션 적용할 Drawable 의 resource 적용 ( animated_logo.xml )

<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_android_black_24dp">

    <target
        android:name="animatedGroup"
        android:animation="@animator/logo_animator" />
</animated-vector>

 

- Drawable 의 logo 파일에 Target 적용할 이름 지어주기 ( ic_android_black_24dp.xml )

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:tint="@android:color/holo_orange_dark"
    android:viewportWidth="24"
    android:viewportHeight="24">

	<group
        android:name="animatedGroup"
        android:pivotX="12"
        android:pivotY="12">
        <path
            android:fillColor="#FF000000"
            android:pathData="M17.6,11.48 L19.44,8.3a0.63,0.63 0,0 0,-1.09 -0.63l-1.88,3.24a11.43,11.43 0,0 0,-8.94 0L5.65,7.67a0.63,0.63 0,0 0,-1.09 0.63L6.4,11.48A10.81,10.81 0,0 0,1 20L23,20A10.81,10.81 0,0 0,17.6 11.48ZM7,17.25A1.25,1.25 0,1 1,8.25 16,1.25 1.25,0 0,1 7,17.25ZM17,17.25A1.25,1.25 0,1 1,18.25 16,1.25 1.25,0 0,1 17,17.25Z" />

    </group>
</vector>

 

- android 31 버전의 themes.xml 에 애니메이션 적용

<style name="Theme.App.Starting" parent="Theme.SplashScreen">
	...
    <item name="android:windowSplashScreenAnimatedIcon">@drawable/animated_logo</item>
</style>

 

 

 

반응형

'Dev > Android' 카테고리의 다른 글

Bluetooth 관련 정리  (0) 2023.03.09
Stream Chat 구현 - Android / 안드로이드  (0) 2022.09.29
Android Studio Dolphin 🐬(2021.3.1) - Stable Release  (0) 2022.09.20
Android MVVM 패턴에 대해서  (0) 2022.09.01
Jetpack Compose 학습  (0) 2022.08.16