每个Android应用程序启动之后都会出现一个Splash启动界面,显示产品LOGO、公司LOGO或者开发者信息。如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间,提高用户体验。


1.splash.xml布局文件
 1 < RelativeLayout  xmlns:android ="http://schemas.android.com/apk/res/android"
 2     xmlns:tools ="http://schemas.android.com/tools"
 3     android:layout_width ="match_parent"
 4     android:layout_height ="match_parent"
 5     tools:context =".SplashActivity"   >
 6
 7      < ImageView
 8          android:layout_width ="match_parent"
 9         android:layout_height ="match_parent"
10         android:background ="@drawable/welcome_android"
11         android:scaleType ="fitCenter"   />
12
13 </ RelativeLayout >

2.SplashActivity类,使用Handler的postDelayed方法,3秒后执行跳转到主视图

 1 package  cn.eoe.leigo.splash;
 2
 3 import  android.app.Activity;
 4 import  android.content.Intent;
 5 import  android.os.Bundle;
 6 import  android.os.Handler;
 7
 8 /** */ /**
 9 * 
10 * @{#} SplashActivity.java Create on 2013-5-2 下午9:10:01    
11 *    
12 * class desc:   启动画面
13 *
14 * <p>Copyright: Copyright(c) 2013 </p> 
15 * @Version 1.0
16 * @Author <a href="mailto:[email protected]">Leo</a>   
17 *  
18 *
19 */

20 public   class  SplashActivity  extends  Activity  {
21
22    //延迟3秒 
23    private static final long SPLASH_DELAY_MILLIS = 3000;
24
25    @Override
26    protected void onCreate(Bundle savedInstanceState) {
27        super.onCreate(savedInstanceState);
28        setContentView(R.layout.splash);
29
30        // 使用Handler的postDelayed方法,3秒后执行跳转到MainActivity 
31        new Handler().postDelayed(new Runnable() {
32            public void run() {
33                goHome();
34            }

35        }
, SPLASH_DELAY_MILLIS);
36    }

37
38    private void goHome() {
39        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
40        SplashActivity.this.startActivity(intent);
41        SplashActivity.this.finish();
42    }

43}

44

3.配置AndroidManifest.xml


<? xml version="1.0" encoding="utf-8" ?>
< manifest  xmlns:android ="http://schemas.android.com/apk/res/android"
    package
="cn.eoe.leigo.splash"
    android:versionCode
="1"
    android:versionName
="1.0"   >

    
< uses-sdk
        
android:minSdkVersion ="10"
        android:targetSdkVersion
="10"   />

    
< application
        
android:icon ="@drawable/logo"
        android:label
="@string/app_name"   >
        
< activity
            
android:name =".SplashActivity"
            android:configChanges
="keyboardHidden"
            android:label
="@string/app_name"
            android:launchMode
="singleTask"
            android:screenOrientation
="portrait"
            android:theme
="@android:style/Theme.NoTitleBar.Fullscreen"   >
            
< 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 >