如何创建启动界面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>

在这里负责注册两个活动。把负责管理启动界面Splash Screen的活动Activity作为应用程序的主活动,然后在SplashScreen中负责启动MyApp。

 

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秒,够用了吧

 

在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            };

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的,给用户以更高的体验。

这样一个基本的Splash就实现了。

posted on 2012-05-09 17:54  jiezzy  阅读(974)  评论(0编辑  收藏  举报