Splash 화면 구현

경로 : res/drawable/splash.xml (생성)

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@color/purple_200" />
  <item>
    <bitmap
      android:src="@drawable/ic_launcher_foreground"
      android:gravity="center"/>
  </item>
</layer-list>

Splash Theme 추가

경로 : res/values/themes.xml ,res/values-night/themes.xml

<!-- 스플래쉬 테마 -->
<style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <item name="colorPrimary">@color/purple_500</item>
  <item name="colorPrimaryVariant">@color/purple_700</item>
  <item name="colorOnPrimary">@color/white</item>
  <!-- Secondary brand color. -->
  <item name="colorSecondary">@color/teal_200</item>
  <item name="colorSecondaryVariant">@color/teal_700</item>
  <item name="colorOnSecondary">@color/black</item>

  <!--타이틀 바 없애기-->
  <item name="windowActionBar">false</item>
  <item name="windowNoTitle">true</item>

  <item name="android:windowBackground">@drawable/splash</item>
</style>

Android Manifest 수정

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="me.eeyo.user">

    <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/SplashTheme"> <!-- Splash Theme 적용 -->
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Main Activity 수정

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_앱이름); // 앱 기본 테마로 변경

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /* 전체화면 코드 시작 */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            final WindowInsetsController insetsController = getWindow().getInsetsController();
            if (insetsController != null) {
                insetsController.hide(WindowInsets.Type.statusBars());
            }
        } else {
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN
            );
        }
        /* 전체화면 코드 끝 */
    }
}

결과물

스플래시 액티비티를 사용하는 것 보다 간결하고, 앱 로딩이 끝나는 즉시 메인 액티비티가 뜬다.

이거 하나 보여주자고 왜 액티비티를 만들어 귀찮게

+ Recent posts