本文實例講述了Android開發基礎之創建啟動界面Splash Screen的方法。分享給大家供大家參考。具體如下:
啟動界面Splash Screen在應用程序是很常用的,往往在啟動界面中顯示產品Logo、公司Logo或者開發者信息,如果應用程序啟動時間比較長,那么啟動界面就是一個很好的東西,可以讓用戶耐心等待這段枯燥的時間。
Android 應用程序創建一個啟動界面Splash Screen非常簡單。比如創建一個工程MySample,主Acitity就叫MySample,創建另一個Activity叫 SplashScreen,用于顯示啟動界面,資源文件為splash.xml。至于如何制作SplashSceen界面,這不是本文章要討論的東西,就 此略過。
SplashScreen的代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
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中修改代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<? 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。
希望本文所述對大家的Android程序設計有所幫助。