使用者工具

網站工具


mobile:android:開發筆記:android創建启動畫面

Android創建启動畫面

http://rritw.com/a/caozuoxitong/OS/20120607/168914.html

http://www.patrickintw.com/blog/splash_screen

Android創建启動畫面
時間:2012-06-07 23:46來源:Internet 作者:Internet 點擊: 605 次
   每個Android應用启動之後都會出現一個Splash启動界面,顯示產品的LOGO、公司的LOGO或者開發者信息。如果應用程序启動時間比較長,那麼启動界面就是一個很好的東西,

   每個Android應用启動之後都會出現一個Splash启動界面,顯示產品的LOGO、公司的LOGO或者開發者信息。如果應用程序启動時間比較長,那麼启動界面就是一個很好的東西,可以讓用戶耐心等待這段枯燥的時間。

  1.制作Splash界面
  突出產品LOGO,產品名稱,產品主要特色;
  注明產品的版本信息;
  注明公司信息或者開發者信息;
  背景圖片,亦可以用背景顏色代替;

  2.處理後台資源
  大多數的Splash界面都是會等待一定時間,然後切換到下一個界面;
  其實,在這段時間裏,可以對系統狀況進行檢測,比如網络是否通,電源是否充足;
  或者,預先加載相關數據;
  为了能讓启動界面展現時間固定,需要計算執行以上預處理任務所花費的時間,那麼:启動界面SLEEP的時間=固定時間-預處理任務時間;

  思路有兩個,一個是用兩個Activity來做,一個用來做启動畫面,另一個是主頁面;還可以用一個Activity來做,程序启動時顯現启動畫面,當启動完成後,它會被隱藏來。

方法一:兩個Activity,通過線程延遲指定的時間再執行Activity的跳轉,我們需要建立兩個Activity,一個是SplashActivity,用來做启動畫面。另一個是HelloWorldActivity。在這裏只是說明如何來做启動畫面,所以就不詳細來完成Activity了。

核心代碼:

package com.demo.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashActivity extends Activity {

	private final int SPLASH_DISPLAY_LENGHT = 6000; // 延遲六秒

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);

		new Handler().postDelayed(new Runnable() {
			public void run() {
				Intent mainIntent = new Intent(SplashActivity.this,
						HelloWorldActivity.class);
				SplashActivity.this.startActivity(mainIntent);
				SplashActivity.this.finish();
			}

		}, SPLASH_DISPLAY_LENGHT);

	}
}

說明:

Handler().postDelayed  是延遲指定的時間再執行

Handler類主要可以使用如下3個方法來設置執行Runnable對象的時間:

//  立即執行Runnable對象  
public final boolean post(Runnable r);  
//  在指定的時間(uptimeMillis)執行Runnable對象  
public final boolean postAtTime(Runnable r, long uptimeMillis);  
//  在指定的時間間隔(delayMillis)執行Runnable對象  
public final boolean postDelayed(Runnable r, long delayMillis); 

下面兩行代碼启動一個新的Activity,同時關閉當前Activity。

SplashActivity.this.startActivity(mainIntent); 
SplashActivity.this.finish();

下面再講一下Activity的生命周期,方便大家理解Activity的跳轉。

image

如上所示,Android 程序員可以决定一個 Activity 的“生”,但不能决定它的“死”,也就時說程序員可以启動一個 Activity,但是卻不能手動的“結束”一個 Activity。

當你調用 Activity.finish()方法時,結果和用戶按下 BACK 鍵一样:告訴 Activity Manager 該 Activity 實例完成了相應的工作,可以被“回收”。

隨後 Activity Manager 激活處於棧第二層的 Activity 並重新入棧,同時原 Activity 被壓入到棧的第二層,從 Active 狀態轉到 Paused 狀態。

例如上面例子中:從 SplashActivity 中启動了 HelloWorldActivity,則當前處於棧頂端的是 HelloWorldActivity,第二層是 SplashActivity 。

當我們調用 SplashActivity.finish()方法時(我們是在SplashActivity中通過SplashActivity.this.finish()調用的),SplashActivity 從 Active 狀態轉換 Stoped 狀態,並被系統從棧中移除,標志可以被“回收”。

Activity 的狀態與它在棧中的位置關系如下圖:

image

 

上圖的例子是

從 Activity1 中启動了 Activity2,則當前處於棧頂端的是 Activity2,第二層是 Activity1,當我們在 Activity2中調用 Activity2.finish()方法時,Activity Manager 重新激活 Activity1 並入棧,Activity2 從 Active 狀態轉換 Stoped 狀態,同時標注Activity2可以被“回收” 。Activity1. onActivityResult(int requestCode, int resultCode, Intent data)方法被執行,Activity2 返回的數據通過 data参數返回给 Activity1。

還有一點要注意的是,不要忘了修改AndroidManifest.xml文件。如下:

<?xml version="1.0" encoding="utf-8"?>
02 	<manifest xmlns:android="http://schemas.android.com/apk/res/android"
03 	      package="com.demo.app" 
04 	      android:versionCode="1" 
05 	      android:versionName="1.0"&gt; 
06 	    <application android:icon="@drawable/icon" ;android:label="@string/app_name"> ;
07 	        <activity android:name=.SplashActivity " 
08 	                  android:label="@string/app_name"> ;
09 	            <intent-filter>
10 	                <action android:name="android.intent.action.MAIN"/> 
11 	                <category android:name="android.intent.category.LAUNCHER" /> 
12 	            </intent-filter> 
13 	        </activity> 
14 	    <activity android:name="Main">
15 	    </activity> 
16 	</application> 
17 	    <uses-sdk android:minSdkVersion="3" /> 
18 	</manifest>

 方法二:一個 Activity來做。程序启動時顯現启動畫面,當启動完成後,它會被隱藏來

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<LinearLayout android:id="@+id/splashscreen"
		android:orientation="vertical" 

                android:layout_width="fill_parent"
		android:layout_height="fill_parent">

		<TextView android:id="@+id/info" 

                        android:layout_width="fill_parent"
			android:layout_height="wrap_content" 

                        android:gravity="center"
			android:paddingTop="10px" 

                        android:text="This is a splash !" />
	</LinearLayout>

	<TextView android:layout_width="fill_parent"
		android:paddingTop="10px" 

                android:layout_height="wrap_content"
		android:text="This is a Context" />
</LinearLayout>


說明:

這裏有一個id为splashscreen的LinearLayout,是程序启動時顯現的部分。當启動完成後,它會被隱藏。

核心代碼:

package com.demo.app;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	private LinearLayout splash;
	private TextView tv;

	private static final int STOPSPLASH = 0;
	// time in milliseconds
	private static final long SPLASHTIME = 1000;

	private Handler splashHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case STOPSPLASH:
				SystemClock.sleep(4000);	
				splash.setVisibility(View.GONE);
					break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		setContentView(R.layout.main);

		splash = (LinearLayout) findViewById(R.id.splashscreen);
		tv = (TextView) findViewById(R.id.info);
		tv.setText("正在建立數據連接");
	
		Message msg = new Message();
		msg.what = STOPSPLASH;
		splashHandler.sendMessageDelayed(msg, SPLASHTIME);
	}
}

當在應用启動後發送一個消息,把指定區域設置为隱藏, splash.setVisibility(View.GONE); 就實現了启動界面。

總結一下,個人覺得還是第一個方法比較合适,當一個程序主Activity裏的東西比較多的時候,如果我們還把启動畫面也加進去,勢必會影響程序的效率,所以實際應用的話,第一個方法是首選。
mobile/android/開發筆記/android創建启動畫面.txt · 上一次變更: 2019/11/16 08:12 (外部編輯)