Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 啟動畫面(Splash)的簡潔實作教學

編輯:關於 Android 程式設計

幾乎每個 Android 應用啟動時都會先閃過一個 Splash 畫面,用來展示產品 LOGO、公司標識或開發資訊。如果應用初始化本身耗時較長,這個畫面的價值就更明顯了——它把一段枯燥的等待變成品牌曝光的機會,也讓使用者更容易耐心等候。

一、splash.xml 佈局檔

 <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".SplashActivity" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/welcome_android" android:scaleType="fitCenter" /> </RelativeLayout> 

二、SplashActivity:用 Handler 延遲三秒跳轉

 package cn.eoe.leigo.splash; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; /** * * @{#} SplashActivity.java Create on 2013-5-2 下午9:10:01 * * class desc: 啟動畫面 * * <p>Copyright: Copyright(c) 2013 </p> * @Version 1.0 * @Author <a href="mailto:gaolei_xj@163.com">Leo</a> * * */ public class SplashActivity extends Activity { //延遲 3 秒 private static final long SPLASH_DELAY_MILLIS = 3000; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); // 使用 Handler 的 postDelayed 方法,3 秒後執行跳轉到 MainActivity new Handler().postDelayed(new Runnable() { public void run() { goHome(); } }, SPLASH_DELAY_MILLIS); } private void goHome() { Intent intent = new Intent(SplashActivity.this, MainActivity.class); SplashActivity.this.startActivity(intent); SplashActivity.this.finish(); } } 

這裡有個容易忽略的細節:goHome() 最後呼叫了 finish()。少了這一行,使用者按返回鍵就會回到啟動頁,體驗上相當突兀。

三、AndroidManifest.xml 配置

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://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> 

關鍵在於把 SplashActivity 設為啟動入口:加上 MAIN 與 LAUNCHER 的 intent-filter,並用全螢幕主題隱藏標題列,畫面才會乾淨。

四、延伸思考

用固定 postDelayed 計時是最直白的做法,但它有兩個潛在問題:一是 Activity 若被銷毀,Handler 的延遲任務仍可能殘留;二是若初始化工作本身就超過三秒,使用者反而會看到卡住的畫面。更嚴謹的實作會把「真正需要等待的初始化任務」與「計時器」結合,並在 onDestroy() 中移除回調。

PS:關於 AndroidManifest.xml 檔案相關屬性功能可參考本站在線工具:

Android Manifest 功能與權限描述大全:
https://tools.jb51.net/table/AndroidManifest

更多 Android 相關內容有興趣的讀者,可參考本站專題:《Android 程式之 activity 操作技巧總結》、《Android 視圖 View 技巧總結》、《Android 操作 SQLite 資料庫技巧總結》、《Android 操作 json 格式資料技巧總結》、《Android 資料庫操作技巧總結》、《Android 檔案操作技巧匯總》、《Android 程式開發之 SD 卡操作方法匯總》、《Android 開發入門與進階教程》、《Android 資源操作技巧匯總》及《Android 控制元件用法總結》。

希望本文所述對大家的 Android 程式設計有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved