🚧

[Android Studio] μŠ€ν”Œλž˜μ‹œ ν™”λ©΄ λ§Œλ“€κΈ° (μ•± 타이틀 ν™”λ©΄, 둜고 ν™”λ©΄)

purpplee 2021. 6. 6. 19:37

μ–΄ν”Œμ„ ν‚€λ©΄ μ œμž‘μ‚¬ λ‘œκ³ κ°€ 잠깐 λ‚˜νƒ€λ‚¬λ‹€κ°€ 사라진 ν›„ 메인 화면이 보인닀.

이 화면을 μŠ€ν”Œλž˜μ‹œ 화면이라고 ν•œλ‹€.

μŠ€ν”Œλž˜μ‹œ 화면은 λ‹¨μˆœνžˆ 보여주기 μš©λ„λ‘œ λ„£λŠ” κ²½μš°λ„ μžˆμ§€λ§Œ, 주둜 μ•±μ—μ„œ ν•„μš”ν•œ λ¦¬μ†ŒμŠ€λ“€μ„ λ‹€μš΄λ°›μ„ λ•Œ λŒ€κΈ° ν™”λ©΄ μš©λ„λ‘œ μ‚¬μš©ν•œλ‹€.

 

λ§Œλ“œλŠ” 방법은 κ°„λ‹¨ν•˜λ‹€. μŠ€ν”Œλž˜μ‹œ μ•‘ν‹°λΉ„ν‹° 화면을 λ”°λ‘œ λ§Œλ“€κ³ , 메인 μ•‘ν‹°λΉ„ν‹°λ‘œ λ„˜μ–΄κ°€λ©΄ λœλ‹€.

 

1. μŠ€ν”Œλž˜μ‹œ μ•‘ν‹°λΉ„ν‹° 생성

첫 ν™”λ©΄μœΌλ‘œ μ‚¬μš©λ˜λ―€λ‘œ 보톡은 제일 처음 λ§Œλ“€μ–΄μ§€λŠ” MainActivity λ₯Ό ν™œμš©ν•œλ‹€.

λ§Œμ•½ λ³„λ„μ˜ μŠ€ν”Œλž˜μ‹œ μ•‘ν‹°λΉ„ν‹°λ₯Ό μƒμ„±ν–ˆλ‹€λ©΄, 첫 화면에 λ„μš°κΈ° μœ„ν•΄ AndroidManifest.xml 에 λ‹€μŒκ³Ό 같이 μž…λ ₯ν•˜μž.

...

<activity android:name=".TitleActivity">
 	<intent-filter>
    	<action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
	</intent-filter>
</activity>

...

intent-filter 뢀뢄이 λ°”λ‘œ ν•΄λ‹Ή μ•‘ν‹°λΉ„ν‹°λ₯Ό 첫 ν™”λ©΄μœΌλ‘œ μ“°κ² λ‹€λŠ” μ˜λ―Έλ‹€.

 

2. 타이틀과 μ•‘μ…˜λ°” μ—†μ• κΈ°

μƒμ„±ν•œ 앑티비티에 타이틀과 μ•‘μ…˜λ°”λ₯Ό μ—†μ• λ³΄μž.

λ¨Όμ € res > values > themes > themes.xml 에 λ‹€μŒμ„ μΆ”κ°€ν•œλ‹€.

...

<style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
</style>

...

 

그리고 λ‹€μ‹œ AndroidManifest.xml  둜 λŒμ•„μ™€μ„œ μ•„λž˜μ²˜λŸΌ μˆ˜μ •ν•œλ‹€.

...

<activity android:name=".TitleActivity" android:theme="@style/SplashTheme">

...

 

3. ν™”λ©΄ 생성

이제 μŠ€ν”Œλž˜μ‹œλ‘œ 보여쀄 화면을 λ§Œλ“€μ–΄λ³΄μž.

 

첫번째 방법은 Theme λ₯Ό ν™œμš©ν•˜μ—¬ background 둜 둜고λ₯Ό λ³΄μ—¬μ£ΌλŠ” 방식이닀.

res > mipmap 에 μ‚¬μš©ν•  이미지λ₯Ό μΆ”κ°€ν•œλ‹€. (drawble 을 μ΄μš©ν•΄λ„ λœλ‹€).

κ·Έ λ‹€μŒ res > values > themes > themes.xml μ—μ„œ μ•„κΉŒ μΆ”κ°€ν–ˆλ˜ 것을 μ•„λž˜μ²˜λŸΌ μˆ˜μ •ν•œλ‹€. λ‚΄κ°€ μΆ”κ°€ν•œ μ΄λ―Έμ§€λŠ” title.png 이닀.

...

<style name="SplashTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        ...
        <item name="android:windowBackground">@mipmap/title</item>

...

이러면 title.png κ°€ ν™”λ©΄ 꽉 차게 λ‚˜νƒ€λ‚œλ‹€.

 

λ‘λ²ˆμ§Έ 방법은 layout xml νŒŒμΌμ„ λ„μš°λŠ” 것이닀. μžμ‹ μ΄ μ›ν•˜λŠ” Layout xml νŒŒμΌμ„ μ“°λ©΄ λœλ‹€.

 

4. μžλ°” μ½”λ“œ

3μ΄ˆλ™μ•ˆ 화면을 보여주고 λ©”μΈν™”λ©΄μœΌλ‘œ λ„˜μ–΄κ°€λŠ” μžλ°” μ½”λ“œλ₯Ό 짜보자.

package //μžμ‹ μ˜ νŒ¨ν‚€μ§€
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class TitleActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_title);	//theme둜 μ§€μ •ν–ˆλ‹€λ©΄ μ‚­μ œν•œλ‹€.
        
        moveMain(1);	//1초 ν›„ main activity 둜 λ„˜μ–΄κ°
    }

    private void moveMain(int sec) {
        new Handler().postDelayed(new Runnable()
        {
            @Override
            public void run()
            {
                //new Intent(ν˜„μž¬ context, 이동할 activity)
                Intent intent = new Intent(getApplicationContext(), MainActivity.class);

                startActivity(intent);	//intent 에 λͺ…μ‹œλœ μ•‘ν‹°λΉ„ν‹°λ‘œ 이동

                finish();	//ν˜„μž¬ μ•‘ν‹°λΉ„ν‹° μ’…λ£Œ
            }
        }, 1000 * sec); // sec초 정도 λ”œλ ˆμ΄λ₯Ό μ€€ ν›„ μ‹œμž‘
    }
}

참고둜 thread.sleep 으둜 delay λ₯Ό μ£Όλ©΄ layout λ‘œλ”©λ„ 같이 λ©ˆμΆ”λ―€λ‘œ n초 ν›„ μ‹€ν–‰λ˜λŠ” postDelayed λ₯Ό μ΄μš©ν•˜μ˜€λ‹€.

 

μ™„μ„±λœ ν™”λ©΄~

λ°˜μ‘ν˜•