tips-programming android splash 로딩 스플래시 안드로이드

안드로이드(android) 앱이 실행되면서 스플래시(splash, 로딩) 화면을 띄우는 소스입니다.

여러가지 방법이 있겠지만 제가 사용하는 방법은 아래와 같습니다.

AndroidManifest.xml



       
           
           
       


       

SplashActivity“/>

MainActivity .java

public class MainActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // 스플래시 화면 띄우기
        startActivity(new Intent(this, SplashActivity.class));

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

        initialize();  // 시간이 걸리는 작업 처리
    }

    /**
     * 스플래시 표시하는 것과 초기화를 동시에 진행시키기 위하여 쓰레드 처리
     *
     */
    private void initialize()
    {
        InitializationRunnable init = new InitializationRunnable();
        new Thread(init).start();
    }

    /**
     * 초기화 작업 처리
     *
     */
    class InitializationRunnable implements Runnable
    {
        public void run()
        {
            // 여기서부터 초기화 작업 처리
            // do_something
        }
    }
}

<p>
  <span style="background-color: rgb(201, 237, 255);">SplashActivity.java</span>
</p>

<blockquote>
  <div style="padding: 10px; background-color: rgb(228, 228, 228);">
    public class <span style="color: rgb(255, 153, 0);">SplashActivity</span> extends Activity<br />{<br />&nbsp; &nbsp; @Override<br />&nbsp; &nbsp; public void onCreate(Bundle savedInstanceState)<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);<br />&nbsp; &nbsp; &nbsp; &nbsp; setContentView(R.layout.splash);</p> 
    
    <p>
      &nbsp; &nbsp; &nbsp; &nbsp; initialize();<br />&nbsp; &nbsp; }
    </p>
    
    <p>
      &nbsp; &nbsp; private void initialize()<br />&nbsp; &nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; Handler handler = &nbsp;&nbsp; new Handler()<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; @Override<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; public void handleMessage(Message msg)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; finish(); &nbsp;&nbsp; // 액티비티 종료<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; }<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; };
    </p>
    
    <p>
      &nbsp; &nbsp; &nbsp; &nbsp; handler.sendEmptyMessageDelayed(0, <span style="color: rgb(255, 0, 0);">3000</span>); &nbsp;&nbsp; // ms, <span style="color: rgb(255, 0, 0);">3초후 종료</span>시킴<br />&nbsp; &nbsp; }<br />}</div> </blockquote> 
      
      <p>
        * 화면을 가로/세로 모드로 전환할 경우 스플래쉬 액티비티가 보이는 문제가 발생할 경우 대처<br />&#8211; 여러가지 방법이 있겠지만 아래처럼 처리하면 됩니다.(2010/10/27 추가)
      </p>
      
      <p>
        <span style="background-color: rgb(201, 237, 255);">AndroidManifest.xml<br /></span>
      </p>
      
      <blockquote>
        <div style="padding: 10px; background-color: rgb(228, 228, 228);">
          <!&#8211; 메인 액티비티 &#8211;><br /><activity android:name=&#8221;MainActivity&#8221; android:label=&#8221;@string/app_name&#8221; <span style="color: rgb(255, 0, 0);">android:configChanges=&#8221;keyboardHidden|orientation&#8221;</span>><br />&nbsp; &nbsp; &nbsp; &nbsp; <intent-filter><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <action android:name=&#8221;android.intent.action.MAIN&#8221; /><br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <category android:name=&#8221;android.intent.category.LAUNCHER&#8221; /><br />&nbsp; &nbsp; &nbsp; &nbsp; </intent-filter><br /></activity></p> 
          
          <p>
            <!&#8211; 스플래시(로딩) 액티비티 &#8211;><br /><activity android:name=&#8221;<span style="color: rgb(255, 153, 0);">SplashActivity</span>&#8220;/></div> </blockquote> 
            
            <p>
              예제 다운로드:<br /><a href="/wp-content/uploads/1/1080351452.zip" class="alignleft" />1080351452.zip</a>
            </p>