모든 개발은 Hello World와 Splash Screen 으로 시작한다.
android {
compileSdkVersion 31
...
}
dependencies {
...
// 2021년 12월 15일 기준 최신 버전
implementation("androidx.core:core-splashscreen:1.0.0-beta01")
}
라이브러리 최신 버전 확인
<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>
<!-- 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>
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/**
* 스플래쉬 스크린 적용
*/
installSplashScreen()
setContentView(R.layout.activity_main)
}
}
setContentView로 첫 frame이 그려지는 순간 SplashScreen은 자동으로 사라진다.
ViewTreeObserver.addOnPreDrawListener 를 사용하여 첫 frame이 그려지는 것을 늦춘다.
true가 리턴될 때까지.
val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener { false }
SplashScreen 기능을 사용하더라도, App의 메인이 되는 Activity 진입 전에,
미리 처리해야하는 작업들이 있기 때문에 이제껏 작성해 왔던 레거시 스타일의
SplashActivity를 삭제할 수 없을 수도 있다.
미리 처리해야 하는 작업이라함은
런처 아이콘을 통해 App이 실행되기까지 그 찰나의 순간 보이는 빈 화면은 이제 더 이상 안 볼 수 있으니까.