Android Splash Screen

UNG·2022년 1월 20일
0

android

목록 보기
1/2
post-thumbnail

모든 개발은 Hello World와 Splash Screen 으로 시작한다.

Ref.

Features
Api Docs
Migration

적용

  • 대략 코드 10줄

build.gradle 설정

android {
   compileSdkVersion 31
   ...
}

dependencies {
    ...
    // 2021년 12월 15일 기준 최신 버전
    implementation("androidx.core:core-splashscreen:1.0.0-beta01")
}

라이브러리 최신 버전 확인

SplashScreen을 위한 테마 추가

  • res/values/theme-splash.xml 파일을 추가한다.
    파일 이름은 원하는대로.
<resources>
<!-- 
    parent는 Theme.SplashScreen을 사용한다.
    name은 자유롭게 지정한다.
-->
    <style name="Theme.App.Starting" parent="Theme.SplashScreen">
        <!--  배경 색상 -->
        <item name="windowSplashScreenBackground">#94b1ff</item>
        <!--  일반 아이콘 또는 애니메이션 아이콘. 보통 런처 아이콘을 사용. -->
        <item name="windowSplashScreenAnimatedIcon">@mipmap/ic_launcher</item>
        <!-- 애니메이션 아이콘 재생 시간. 애니메이션 아이콘일 경우 필수 -->
        <!-- SplashScreen이 표시되고 사라지는 시간에는 영향을 주지 않는다. -->
        <item name="windowSplashScreenAnimationDuration">300</item>
        <!-- SplahScreen이 사라진 후 Activity에 적용할 테마를 지정한다. -->
        <item name="postSplashScreenTheme">@style/Theme.CoreSplash</item>
    </style>
</resources>

SplashScreen을 표시할 Activity의 테마 설정

<!-- application의 테마는 일반 테마를 사용한다. -->
<!-- MainActivity에 추가한 Theme.App.Starting를 적용 -->
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.CoreSplash">
  
    <activity
    	android:name=".MainActivity"
    	android:exported="true"
        android:noHistory="true"
    	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>

SplashScreen을 표시할 Activity 설정

  • installSplashScreen()를 호출
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        /**
         * 스플래쉬 스크린 적용
         */
        installSplashScreen()

        setContentView(R.layout.activity_main)
    }
}

SplashScreen이 사라지는 시점

setContentView로 첫 frame이 그려지는 순간 SplashScreen은 자동으로 사라진다.

SplashScreen을 더 오래 표시하려면

ViewTreeObserver.addOnPreDrawListener 를 사용하여 첫 frame이 그려지는 것을 늦춘다.
true가 리턴될 때까지.

val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener { false }

SplashScreen이 SplashActivity를 계승?

SplashScreen 기능을 사용하더라도, App의 메인이 되는 Activity 진입 전에,
미리 처리해야하는 작업들이 있기 때문에 이제껏 작성해 왔던 레거시 스타일의
SplashActivity를 삭제할 수 없을 수도 있다.

미리 처리해야 하는 작업이라함은

  • App의 버전 체크 후 업데이트
  • 기초 데이터 초기화
  • 딥링크 처리
  • 기타 등등

그래도 SplashScreen을 사용해야 하는 이유?

런처 아이콘을 통해 App이 실행되기까지 그 찰나의 순간 보이는 빈 화면은 이제 더 이상 안 볼 수 있으니까.

profile
우웅

0개의 댓글