-
Android 12 Splash Screen (스플래시 스크린)Android 2021. 12. 9. 00:34반응형
https://developer.android.com/guide/topics/ui/splash-screen
Splash screens | Android Developers
Splash screens Important: If you have previously implemented a custom splash screen in Android 11 or lower, you’ll need to migrate your app to the SplashScreen API to ensure that it displays correctly in Android 12 and higher. For instructions, see Migra
developer.android.com
안드로이드12 부터 스플레시 스크린(Splash Screens) 가 추가되었습니다.
해당기능은 콜드스타트(Cold start) 나 웜 스타트(Warm start)에서 앱이 로드되는 동안 표시되는 화면 입니다.
콜드 스타트(Cold start) ,웜 스타트(Warm start), 핫 스타트(Hot start) 에 대한 내용은 아래의 앱시작시간에 대한 링크를 참고하시기 바랍니다.
https://developer.android.com/topic/performance/vitals/launch-time
앱 시작 시간 | Android 개발자 | Android Developers
앱 시작 시간 사용자는 앱이 응답하고 빠르게 로드되기를 기대합니다. 시작 시간이 느린 앱은 이 기대를 충족하지 못하며 사용자에게 실망스러울 수 있습니다. 이러한 종류의 좋지 못한 경험으
developer.android.com
시작예시 https://github.com/DNights/Android12SplashScreenSample
GitHub - DNights/Android12SplashScreenSample
Contribute to DNights/Android12SplashScreenSample development by creating an account on GitHub.
github.com
안드로이드 12부터 사용할수 있는 기능이므로 API 31로 themes.xml 파일을 생성합니다.
<!-- Customize SplashScreen here. --> <item name="android:windowSplashScreenBackground">@color/purple_700</item> <item name="android:windowSplashScreenAnimatedIcon">@drawable/jetpack_compose_icon</item> <item name="android:windowSplashScreenAnimationDuration">1000</item> <item name="android:windowSplashScreenIconBackgroundColor">@color/teal_700</item> <item name="android:windowSplashScreenBrandingImage">@drawable/ic_launcher_foreground</item>
그리고 위와 같이 각 옵션을 추가하여 할당 할 수 있습니다.
위의 셋팅이 없을경우 기본적인 앱 아이콘이 표기되는 스플래시 스크린이 표기 됩니다.
android:windowSplashScreenBackground 는 전체 배경색입니다.
android:windowSplashScreenAnimatedIcon 는 가운대 표기되는 아이콘 이미지 입니다.
android:windowSplashScreenAnimationDuration 는 해당 스플래시 스크린이 표기 되는 시간 입니다.
android:windowSplashScreenIconBackgroundColor 는 가운대 표기는 아이콘 뒤의 원형의 배경색입니다.
android:windowSplashScreenBrandingImage 는 하단의 표기 이미지 입니다.
위의 코드를 실행 하면 아래와 같은 스플래시 스크린이 표기 됩니다.
스플래시 이미지를 표기하고 종료하는것을 고정된 시간이 아닌 조건에 따라서 코드로 조절 할 수 있습니다.
..... var isReady = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) thread(start=true) { for (i in 1..5) { Thread.sleep(1000) } isReady = true } // Set up an OnPreDrawListener to the root view. val content: View = findViewById(android.R.id.content) content.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { override fun onPreDraw(): Boolean { // Check if the initial data is ready. return if (isReady) { // The content is ready; start drawing. content.viewTreeObserver.removeOnPreDrawListener(this) true } else { // The content is not ready; suspend. false } } } ) ......
위의 코드와 같이 content라는 view에 addOnPreDrawListener 를 통해서 조건을 통해서 onPreDraw 의 return 값을 내려주는 것으로 조절 할 수 있습니다.
예시의 코드에서는 isReady의 조건으로 스플래시 스크린 표기 조건을 컨트롤 하고 있습니다.
그리고 스플래시 스크린이 끝난뒤 다른 작업을 처리하거나 다음화면을 표기하는 애니메이션을 셋팅하는 등의 처리는
splashScreen.setOnExitAnimationListener 를 통해서 스플래시 스크린 애니메이션의 종료시점을 받고 처리를 할수 있습니다.
...... // Add a callback that's called when the splash screen is animating to // the app content. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { splashScreen.setOnExitAnimationListener { splashScreenView -> // Create your custom animation. val slideUp = ObjectAnimator.ofFloat( splashScreenView, View.TRANSLATION_Y, 0f, -splashScreenView.height.toFloat() ) slideUp.interpolator = AnticipateInterpolator() slideUp.duration = 200L // Call SplashScreenView.remove at the end of your custom animation. slideUp.doOnEnd { splashScreenView.remove() } // Run your animation. slideUp.start() } ......
그리고 스플래시 스크린의 애니메이션의 재생시간등의 정보는 아래와 같이 얻을 수 있습니다.
....... // Get the duration of the animated vector drawable. val animationDuration = splashScreenView.iconAnimationDuration // Get the start time of the animation. val animationStart = splashScreenView.iconAnimationStart // Calculate the remaining duration of the animation. val remainingDuration = if (animationDuration != null && animationStart != null) { (animationDuration - Duration.between(animationStart, Instant.now())) .toMillis() .coerceAtLeast(0L) } else { 0L } Log.e("test", "animationDuration - $remainingDuration") Log.e("test", "animationStart - $remainingDuration") Log.e("test", "remainingDuration - $remainingDuration") ......
MainActivity 전체 코드
class MainActivity : AppCompatActivity() { var isReady = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) thread(start=true) { for (i in 1..5) { Thread.sleep(1000) } isReady = true } // Set up an OnPreDrawListener to the root view. val content: View = findViewById(android.R.id.content) content.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { override fun onPreDraw(): Boolean { // Check if the initial data is ready. return if (isReady) { // The content is ready; start drawing. content.viewTreeObserver.removeOnPreDrawListener(this) true } else { // The content is not ready; suspend. false } } } ) // Add a callback that's called when the splash screen is animating to // the app content. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { splashScreen.setOnExitAnimationListener { splashScreenView -> // Get the duration of the animated vector drawable. val animationDuration = splashScreenView.iconAnimationDuration // Get the start time of the animation. val animationStart = splashScreenView.iconAnimationStart // Calculate the remaining duration of the animation. val remainingDuration = if (animationDuration != null && animationStart != null) { (animationDuration - Duration.between(animationStart, Instant.now())) .toMillis() .coerceAtLeast(0L) } else { 0L } Log.e("test", "animationDuration - $remainingDuration") Log.e("test", "animationStart - $remainingDuration") Log.e("test", "remainingDuration - $remainingDuration") // Create your custom animation. val slideUp = ObjectAnimator.ofFloat( splashScreenView, View.TRANSLATION_Y, 0f, -splashScreenView.height.toFloat() ) slideUp.interpolator = AnticipateInterpolator() slideUp.duration = 200L // Call SplashScreenView.remove at the end of your custom animation. slideUp.doOnEnd { splashScreenView.remove() } // Run your animation. slideUp.start() } } } }
이전버전에 스플래시 스크린을 적용하였다면 안드로이드 12버전에 맞도록 마이그래이션 하여야 합니다.
https://developer.android.com/guide/topics/ui/splash-screen/migrate
Migrate your existing splash screen implementation to Android 12 and higher
Migrate your existing splash screen implementation to Android 12 and higher If you have previously implemented a custom splash screen in Android 11 or lower, you’ll need to migrate your app to the SplashScreen API to ensure that it displays correctly in
developer.android.com
[참고링크]
https://stackoverflow.com/questions/69010523/android-12-splash-screen-before-api-21
Android 12 splash screen before api 21
I'm trying to upgrade my application to target android 31 which introduces splash screen API so I followed the migration process mentioned here, but after migration the application doesn't run beca...
stackoverflow.com
https://codechacha.com/ko/android-12-splash-screens/
Android 12 - Splash Screens 알아보기
Splash Screen은 App이 처음 실행될 때, 로딩되는 화면을 가리기 위해 만드는 화면입니다. Android 12 이전에는 개발자가 직접 페이지를 만들었지만, Android 12(SDK 31)에서 동작하는 App은 기본적으로 Splash S
codechacha.com
https://joebirch.co/android/exploring-android-12-splash-screen/
Exploring Android 12: Splash Screen
With the Android 12 beta now available, we're starting to learn more about the new features that the latest version of Android gives to us. One of the things that caught my eye here is the introduction of a Splash Screen API - not only providing a standard
joebirch.co
반응형'Android' 카테고리의 다른 글
Jetpack Compose Modifier.onFocusChanged에서 animateScrollTo 가 작동안되는 문제 (0) 2022.04.07 Null Coalescing Operator(with. databinding) (0) 2022.01.05 Modifier in Jetpack Compose (0) 2021.12.15 Android adb 사용 여부 앱에서 확인하는 법 (0) 2021.12.14 Android studio Gradle 7.0 upgrade (0) 2021.07.04 Jetpack Compose Setting (0) 2021.06.30 Android Sticky Header RecyclerView (0) 2021.06.17 Android Studio 자동완성 기능 안될경우 (with. MAC) (3) 2021.04.29