如何建立啓動界面Splash Screen

如何建立啓動界面Splash Screen

啓動界面Splash Screen在應用程序是很經常使用的,每每在啓動界面中顯示產品Logo、公司Logo或者開發者信息,若是應用程序啓動時間比較長,那麼啓動界面就是一個很好的東西,能夠讓用戶耐心等待這段枯燥的時間。

Android 應用程序建立一個啓動界面Splash Screen很是簡單。好比建立一個工程MySample,主Acitity就叫MySample,建立另外一個Activity叫 SplashScreen,用於顯示啓動界面,資源文件爲splash.xml。

SplashScreen的代碼以下:
package com.ctoof.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;

public class SplashScreen extends Activity {
    protected boolean _active = true;
    protected int _splashTime = 5000;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        
        Thread splashTread = new Thread() {
            @Override
            public void run() {
                try {
                    int waited = 0;
                    while(_active && (waited < _splashTime)) {
                        sleep(100);
                        if(_active) {
                            waited += 100;
                        }
                    }
                } catch(InterruptedException e) {
                    // do nothing
                } finally {
                    finish();
                    // 啓動主應用
                    startActivity(new Intent("com.ctoof.android.MySample.MyApp"));
                    stop();
                }
            }
        };
        splashTread.start();
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            _active = false;
        }
        return true;
    }
}
而後在AndroidMainfest.xml中修改代碼以下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.ctoof.android"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SplashScreen"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MyApp">
            <intent-filter>
                <action android:name=" com.ctoof.android. MySample.MyApp " />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>

html

在這裏負責註冊兩個活動。把負責管理啓動界面Splash Screen的活動Activity做爲應用程序的主活動,而後在SplashScreen中負責啓動MyApp。 android

 

new Handler().postDelayed(new Runnable(){   // 爲了減小代碼使用匿名Handler建立一個延時的調用
            public void run() {  
                Intent i = new Intent(SplashScreen.this, Main.class);    //經過Intent打開最終真正的主界面Main這個Activity
                SplashScreen.this.startActivity(i);    //啓動Main界面
                SplashScreen.this.finish();    //關閉本身這個開場屏
            }  
        }, 5000);   //5秒,夠用了吧
app

 

在Activity中的onCreate方法中,初始化並開始Timer:
1    timer = new Timer(true);
2    startTime = System.currentTimeMillis();
3    timer.schedule(task, 0, 1);
startTime是開始時間,要判斷時間差是否知足設定的時間。下面是TimerTask的代碼:
01    private final TimerTask task = new TimerTask() {
02                    @Override
03                    public void run() {
04                           if (task.scheduledExecutionTime() - startTime == 1000 || !_active) {
05                                             Message message = new Message();  
06                                             message.what = 0;
07                                             timerHandler.sendMessage(message);
08                                             timer.cancel();
09                                             this.cancel();
10                                          }
11    
12                    }
13            };
還有handler的代碼:
01    private final Handler timerHandler = new Handler() {
02                    public void handleMessage(Message msg) {
03                            switch (msg.what) {  
04                case 0:
05                 
06                        SplashScreen.this.finish();
07                            //start new activity here
08                        break;
09                            }  
10                            super.handleMessage(msg);  
11                    }
12            };
ide

1    @Override
2        public boolean onTouchEvent(MotionEvent event) {
3            if (event.getAction() == MotionEvent.ACTION_DOWN) {
4                _touched = false;
5            }
6            return true;
7        }

在顯示Splash Screen的過程當中,若是觸摸了屏幕,會直接跳過Splash Screen的,給用戶以更高的體驗。
post

這樣一個基本的Splash就實現了。 this

相關文章
相關標籤/搜索