編輯:關於 Android 程式設計
幾乎每個 Android 應用啟動時都會先閃過一個 Splash 畫面,用來展示產品 LOGO、公司標識或開發資訊。如果應用初始化本身耗時較長,這個畫面的價值就更明顯了——它把一段枯燥的等待變成品牌曝光的機會,也讓使用者更容易耐心等候。
<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>
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()。少了這一行,使用者按返回鍵就會回到啟動頁,體驗上相當突兀。
<?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 程式設計有所幫助。
Android 筆記(八):Intent 元件的各種用法與 APK 反編譯
前面幾節已經談過 Intent 的作用、屬性以及 Activity 之間的值傳遞,這一節把重點放在「怎麼用」。在我的語音辨識專案裡,就是靠 Intent 來啟動 App
Android NDK 開發入門:從環境搭建到 JNI 雙向調用與第三方 so 集成
前言:我對 C/C++ 可以說毫無基礎,大學時學過一個學期的 C,考完試也基本還給老師了。這篇文章是我完整做過一個 NDK 項目之後積累下來的筆記,定位是小白友好型教程,
Android Spinner 下拉清單:兩種寫法與五個必要步驟
Spinner 就是把選單攤開讓使用者挑一項,看似簡單,但適配器的選擇會直接影響你能客製到什麼程度。以下整理五個必要步驟,以及實務上最常見的兩種寫法。 五個步驟,缺
ViewPager + Fragment + ListView 組合實戰
本文記錄一個常見組合:ViewPager + Fragment + ListView,配合 PagerSlidingTabStrip 實現頂部可滑動的 Tab 分類切換