본문 바로가기

안드로이드 앱 개발

안드로이드 프로그래밍-스플래시 화면(로딩화면)

728x90
반응형

우선 앱의 기능을 구현하기 전에 스플래시 화면을 만들고 싶어졌다.



스플래시 화면이란 쉽게 말하면 로딩 화면과 같다고 보면 된다.


앱을 시작하면 애플리케이션의 규모?에 따라 

메인 액티비티의 로딩 시간이 길어지게 될 텐데



이를 기다리기에는 사용자가 앱이 제대로 실행이 되어 

로딩 중인 것인지 에러가 난 것인지 알 수가 없다!ㅠㅠ(그리고 지루함)


카카오톡이나 인스타그램, 업비트 앱 등 처음 앱이 실행될 때 

앱의 로고가 화면에 출력되는 것을 본적이 있을 것이다.


앱이 메모리에 올라와 있으면 이후 다시 실행할 때는 

이 스플래시 화면이 안보이거나 매우 짧게 보이는 것을 볼 수 있었을 것이다!



스플래시 화면을 구성하기 위해서는 크게 두 가지가 있다.




                 1. 핸들러를 만들어 일정 시간 후에 스플래시 화면에서 메인 화면으로 넘어가는 방법.


                 2. 그리고 핸들러를 사용하지 않고 바로 메인 화면으로 넘어가는 방법이 있다.


         첫 번째 방법은 별도의 레이아웃을 만들어 setContentView로 설정하는 것이고


         두 번째 방법은 테마로 정의하는 방법이다.




한번 스플래시 화면을 만들기 위한 과정을 살펴보자!


테마로 정의하는 방법은 다음과 같다.




패키지를 선택하여 마우스 오른쪽 버튼을 누른 뒤 

new → Java Class로 스플래시를 위한 새로운 클래스를 정의해준다.


클래스 생성시에 Superclass는 android.support 패키지의 

AppCompatActivity를 상속하도록 설정해준다.





1. SplashActivity.java 작성


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class SplashActivity extends AppCompatActivity {
   // Handler handler = new Handler();
 
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        startActivity(intent);
       /* handler.postDelayed(new Runnable() {
            @Override
            public void run() {
            Intent intent = new Intent(getApplicationContext(),MainActivity.class);
                    startActivity(intent);
                    finish();
            }
        },1000);*/
       finish();
    }
}


SplashActivity.java이다


주석 처리된 부분은 지연 시간을 주기 위한 핸들러를 준 부분이고


앱 로딩이 완료되면 바로 띄우기 위해 지연 시간을 따로 주지 않았다.




2. AndroidManifest.xml 수정



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
...
    <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/AppTheme">
        <activity android:name=".SplashActivity"
            android:theme="@style/SplashTheme">
            <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>
cs
 위 코드에서 두 개의 activity 태그가 정의되어 있는 것을 볼 수 있다.


 


<intent-filter>가 선언된 부분이 제일 처음 시작되는 액티비티이므로 


액티비티를 SplashActivity로 바꿔보도록하자.


그리고 10 번째 줄을 보면 android:theme="@style/SplashTheme"이라는 걸 볼 수 있는데


테마는 res폴더 -> values -> styles.xml에 정의되어있다.



1
2
3
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
cs


다음과 같이 style 태그를 추가하고 style의 name 속성은  


매니페스트 파일에서 해당 액티비티의 테마 이름과 일치시켜주도록한다.


이 코드는 백그라운드를 drawable폴더의 splash_background라는 속성을 참고한다.





3.drawable 폴더에 splash_background 생성.


1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/splash_base"></item>
 
    <item android:top="210dp">
        <bitmap android:src="@drawable/icontest" android:gravity="top"/>
    </item>
</layer-list>
cs

drawable 폴더에 원하는 아이콘을 집어넣고 해당 이미지를 bitmap 태그로 불러온다.



4.splash_base 생성


1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:startColor="#FF3E50B4"
    android:centerColor="#FF7288DB"
    android:endColor="#FF7288DB"
    android:angle="90"
    android:centerY="0.5" />
 
    <corners android:radius="0dp"/>
 
</shape>
cs

drawable 폴더에 gradient 태그를 주어 그라데이션 효과를 주었다.



이제 프로젝트를 빌드 하고 실행시켜보자!


728x90