프로젝트 생성부터 론치스크린 구현까지
안드로이드 스튜디오 > 새 프로젝트 Empty Activity 로 프로젝트 생성 안드로이드 버전은 점유율 확인하고 90% 근처 올 정도의 버전을 선택 5 롤리팝 API 21 선택!
론치화면의 구성
배경이미지 + 로고이미지 launch_screen_bg.png launch_screen_logo.png 이미지 사이즈는 적당한 고해상도로 준비
새 Empty Activity를 하나 추가한다. 이름은 LaunchScreenActivity Generate a Layout File 은 체크하지 않는다. Layout은 만들지 않도록 한다. Launcher Activity 는 체크!
AndroidManifest.xml에 가서 원래 있던 MainActivity의 intent filter를 삭제한다. 이제 처음 시작을 LaunchScreenActivity에 넘겨주는거다.
LaunchScreenActivity를 론치 액티비티로 만들어줘야한다. 이후 만들 스타일로 미리 지정해둔다.
<activity android:name=".LauncherScreenActivity"
android:label=" "
android:theme="@style/LaunchScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
LaunchScreenActivity.kt 에 가서
onCreate() 안에
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
MainActivity가 로딩이 되면 바로 넘어가도록 작성한다.
res/values/themes.xml 에 가서
<!-- For Launch Screen -->
<style name="LaunchScreenTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:background">@drawable/launch_screen_background</item>
<item name="android:windowFullscreen">true</item>
<item name="windowNoTitle">true</item>
</style>
LaunchScreen 을 위한 스타일을 만든다.
론치스크린은 현재 layout 이 없는데, 스타일에서 배경을 설정하는 방법을 사용한다.
drawable 에 launch_screen_background.xml 을 만든다. 위에 스타일에 설정된 drawable 되시겠다.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/launch_screen_background_gradient"
android:top="0dp"
android:bottom="0dp"
android:left="0dp"
android:right="0dp" />
<item
android:drawable="@drawable/launch_screen_logo"
android:width="167dp"
android:height="76dp"
android:gravity="center">
</item>
</layer-list>
첫번째 아이템인 launch_screen_background_gradient 는 배경에 그레디언트를 넣기위해 만들어 둔 drawable xml이다. 보통의 이미지 파일을 사용해서 배경을 가득 채울 수도 있다. 두번째 아이템 launch_screen_logo가 화면 가운데에 보여지게된다.
그레디언트 방법
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="90"
android:startColor="#ff9300"
android:endColor="#ff0900" />
</shape>
여기까지하면 레이아웃 없이 스타일 설정만으로 론치화면을 구현할 수 있다. 하지만, 상단의 상태바가 그대로 보이는 기종이 있을 수도 있다고 하니, 코드로 감추는 작업을 한다.
LaunchScreenActivity.kt > onCreate() 에 넣어준다.
// Hide the status bar.
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN
// Remember that you should never show the action bar if the
// status bar is hidden, so hide that too if necessary.
actionBar?.hide()