編輯:關於 Android 開發
現在打開任一款 App,大部分都會顯示一個啟動畫面,展示公司的 Logo 與當前版本,有的乾脆把廣告放上去。啟動畫面可以分為兩種設定方式:一種用兩個 Activity 實現,另一種用一個 Activity 實現。以下分別介紹這兩種做法。
一、兩個 Activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
public class SplashActivity extends Activity {
private static int SPLASH_DISPLAY_LENGHT = 6000; // 延遲 6 秒
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE); // 去掉標題
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
SplashActivity.this.finish(); // 關閉並回收 SplashActivity,否則按返回鍵會回到此頁
}
}, SPLASH_DISPLAY_LENGHT);
}
}
別忘了設定 AndroidManifest.xml:
<activity android:name="com.example.andorid_splash_0.SplashActivity" android:label="splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" android:label="@string/app_name">
</activity>
不難看出:SplashActivity 先於 MainActivity 啟動,經過 6 秒後才啟動 MainActivity。
補充一點知識(Handler 的幾個常用方法):
// 立即執行 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 啟動
先看佈局檔,裡面放了兩個充滿螢幕的元件(一個 ImageView 與一個 TextView):
<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:orientation="vertical">
<LinearLayout
android:id="@+id/splashScreen"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/new00"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="100dp"
android:gravity="center"
android:text="主界面"/>
</LinearLayout>
Activity 程式碼:
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.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout splash;
private ImageView iv_image;
private static final int STOPSPLASH = 0;
private static final long SPLASHTIME = 1000;
private Handler splashHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
SystemClock.sleep(4000); // 休眠 4 秒
splash.setVisibility(View.GONE);
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
splash = (LinearLayout) findViewById(R.id.splashScreen);
Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME); // 在 SPLASHTIME 時間後發送訊息
}
}
三、總結:
以上兩種方法都能實現應用啟動前的開機畫面,但在實際開發中還是建議採用第一種,因為主界面的程式碼不宜過多,應當保持簡潔。
Android 效能分析入門:Traceview 介面解讀與追蹤日誌實作
Traceview 是什麼 Traceview 是執行日誌的圖形化檢視器,資料來源是透過 Debug 類別記錄的方法追蹤。它能幫你看清楚一段時間內每個函式被呼叫幾次、各自
AlertDialog 六種對話框用法詳解:從簡單文本到自定義 View
對話框的四個區域 AlertDialog 生成的對話框由四個區域組成:圖標區、標題區、內容區、按鈕區。理解這個結構,後面的 API 就好記了。 標準使用步驟 第一步,創
Android 程式碼故事 第一回:那些等間距排列的按鈕
App 新做了一個放操作按鈕的畫面,老闆的要求是「簡潔美觀有內涵,按鈕要均勻分布」。設計 MM 給了一張圖,示意起來大概是這樣: |===================
Android 撥號器簡單實現
撥號功能實現撥號功能可透過 Intent 完成。畫面中放置輸入框與按鈕,點擊後將號碼傳給系統撥號程式。版面配置使用 LinearLayout 垂直排列 TextView、