編輯:關於 Android 程式設計
裝好一個 App 首次打開,映入眼簾的往往就是那張 Splash Screen。撇開 Material Design 對這種做法的態度不談,它至少解決了兩個實際問題:一是新裝應用的品牌曝光與新手指引,二是版本更新時的功能亮點說明。有人對它嗤之以鼻,有人趨之若鶩,好壞不在本文討論範圍,這裡只談怎麼寫。

如果歡迎頁只需要展示 Logo、版本號這類靜態信息,那麼做法非常直白——讓 WelcomeActivity 停留幾秒,然後自動跳轉到主界面。Handler.postDelayed() 就能勝任:
// 3 秒後啟動主界面
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish(); // 啟動主 Activity 後銷毀自身,避免返回鍵回到歡迎頁
}
}, 3000);
別忘了那句 finish(),否則用戶按返回鍵會退回閃屏,體驗很怪。
更常見的場景是:App 在進入主界面前必須完成一些初始化工作,比如從網路拉取配置、下載圖片、解析 JSON/XML、上報設備信息、校驗登入態等等。這時 Splash 就成了「合法的等待時間」——把耗時任務藏進去,完成後再放行。
下面以「遠程下載一張圖片,解析後展示在主界面」為例。
<LinearLayout 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"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/appImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/logo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:text="Welcome"
android:textColor="#00ACED"
android:textSize="30sp" />
</LinearLayout>
佈局效果:

只放一個 ImageView,用來展示下載下來的圖片:
<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"
android:gravity="center">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
用 AsyncTask 在後台完成下載與解析,再通過 Intent 把結果傳給 MainActivity:
public class SplashScreenActivity extends Activity {
private static final String URL = "https://www.jycoder.com/json/Image/1.jpg";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
/* 簡單版 Splash 的寫法(保留備用)
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, 3000);
*/
// 後台執行下載任務
new FetchDataTask().execute(URL);
}
public class FetchDataTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
try {
HttpURLConnection connection =
(HttpURLConnection) (new URL(params[0])).openConnection();
InputStream is = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
Intent intent = new Intent(SplashScreenActivity.this, MainActivity.class);
// 注意:Intent 傳遞 Bitmap 時,圖片大小不宜超過 40K
intent.putExtra("Image", result);
startActivity(intent);
finish(); // 同樣要銷毀自身
}
}
}
public class MainActivity extends Activity {
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image);
Intent intent = getIntent();
if (intent != null) {
Bitmap bitmap = intent.getParcelableExtra("Image");
imageView.setImageBitmap(bitmap);
}
}
}
記得加網路權限,並把 SplashScreenActivity 設為啟動項:
<manifest package="com.coder.splashscreen"
xmlns:android="https://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>
</manifest>
我第一次用遠程圖片測試時直接崩了,報的是 TransactionTooLargeException 一類的錯誤:

原因是 Intent 附带的 Bundle 大小受限,傳 Bitmap 時不宜超過 40K。生產環境中更推薦的做法是:把圖片寫入本地檔案或傳 URI/路徑,主界面再去讀取,或者乾脆交給成熟的圖片加載庫處理。
完成後的效果:

例子都不算複雜,但 Splash Screen 的思路基本就這兩類:要麼純定時跳轉,要麼「後台幹活 + 完成後跳轉」。真正需要斟酌的其實不是代碼,而是該不該放、放多久——超過三五秒的閃屏,只會讓用戶想卸載。
Android Fragment 與 Activity 傳值:setArguments 與 Bundle 實作
使用 setArguments 傳值 Activity 可建立 Bundle,將資料放入後透過 Fragment 的 setArguments 傳入。Fragment 再
仿微信模糊搜索:EditText 動態過濾 ListView 並高亮匹配
模糊搜索框是 App 中很常見的需求,本文以安卓端為例,實現一個仿微信的模糊搜索效果。 先看上圖,效果還不錯。圖一為未點擊狀態,圖二為點擊之後的狀態,圖三為輸入之後的狀態
微信公眾號開發:服務器配置、Token 驗證與訊息回覆
微信公眾號開發準備若要透過程式碼實作微信公眾號的自動回覆或自訂功能,必須先準備可公開存取的服務器。後續所有訊息都會由微信伺服器轉發到該服務器。進入公眾號後台後,開啟基本配
Android 入門實作:手把手做出一個簡易計算器
動手做一個計算器,我把它拆成兩大部分:界面設計與功能實作。兩者其實緊密相連——界面怎麼設計,功能就得怎麼配合。 (成品效果圖) 一、界面設計 元件:界面共 1