每個Android應用啟動之後都會出現一個Splash啟動介面,顯示產品的LOGO、公司的LOGO或者開發人員資訊。如果應用程式啟動時間比較長,那麼啟動介面就是一個很好的東西,可以讓使用者耐心等待這段枯燥的時間。
- 製作Splash介面
突出產品LOGO,產品名稱,產品主要特色;
註明產品的版本資訊;
註明公司資訊或者開發人員資訊;
背景圖片,亦可以用背景顏色代替;
- 除了等待還能做點什麼
大多數的Splash介面都是會等待一定時間,然後切換到下一個介面;
其實,在這段時間裡,可以對系統狀況進行檢測,比如網路是否通,電源是否充足;
或者,積極式載入相關資料;
為了能讓啟動介面展現時間固定,需要計算執行以上預先處理任務所花費的時間,那麼:啟動介面SLEEP的時間=固定時間-預先處理任務時間
- 源碼樣本(以Wordpress的Android用戶端為例)
AndroidMenifest.xml <activity android:icon="@drawable/app_icon"
android:screenOrientation="portrait"
android:name=".splashScreen"
android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
splashScreen.java
package org.wordpress.android;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.TextView;
public class splashScreen extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.splashscreen);
//Display the current version number
PackageManager pm = getPackageManager();
try {
PackageInfo pi = pm.getPackageInfo("org.wordpress.android", 0);
TextView versionNumber = (TextView) findViewById(R.id.versionNumber);
versionNumber.setText("Version " + pi.versionName);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
public void run() {
/* Create an Intent that will start the Main WordPress Activity. */
Intent mainIntent = new Intent(splashScreen.this, wpAndroid.class);
splashScreen.this.startActivity(mainIntent);
splashScreen.this.finish();
}
}, 2900); //2900 for release
}
}
splashscreen.xml
<!--
android:gravity是對元素本身說的,元素本身的文本顯示在什麼地方靠著換個屬性設定,不過不設定預設是在左側的。
android:layout_gravity是相對與它的父元素說的,說明元素顯示在父元素的什麼位置
-->
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center|center"
android:background="@drawable/home_gradient"
android:orientation="vertical">
<!--
android:scaleType是控製圖片如何resized/moved來匹對ImageView的size
CENTER_INSIDE / centerInside 將圖片的內容完整置中顯示,通過按比例縮小或原來的size使得圖片長/寬等於或小於View的長/寬
-->
<ImageView android:layout_marginTop="-60dip"
android:paddingLeft="20dip"
android:paddingRight="20dip"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/wordpress_logo"
android:src="@drawable/wordpress_home">
</ImageView>
<!--
android:typeface 字型風格
-->
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:typeface="serif"
android:shadowDx="0"
android:shadowDy="2"
android:shadowRadius="1"
android:shadowColor="#FFFFFF"
android:textColor="#444444"
android:textSize="20dip"
android:id="@+id/versionNumber"
android:gravity="bottom">
</TextView>
</LinearLayout>