萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> Android 開發

Android Splash 啟動頁實作兩大方案:雙 Activity vs 單 Activity 視圖切換

啟動頁(Splash)常見需求:顯示品牌 Logo、初始化資料、延遲幾秒進入主介面。兩種主流實作方式各有優缺點。

方案一:雙 Activity + Handler 延遲跳轉(傳統寫法)

AndroidManifest.xml

<activity android:name=".SplashActivity"
    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" />

SplashActivity.java

public class SplashActivity extends Activity {
    private static final int SPLASH_DURATION = 3000; // 3 秒

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash); // 包含 Logo ImageView 等

        new Handler().postDelayed(() -> {
            startActivity(new Intent(this, MainActivity.class));
            finish(); // 關閉 Splash,避免 Back 返回
        }, SPLASH_DURATION);
    }
}

優點:邏輯清晰、生命週期分離。缺點:啟動兩個 Activity 稍增開銷,需在 Manifest 註冊兩個入口。

方案二:單 Activity + View 顯隱動畫(輕量、無閃爍)

layout/activity_main.xml

<FrameLayout xmlns:android="..." android:layout_width="match_parent" android:layout_height="match_parent">
    <!-- 主內容 -->
    <WebView android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" />

    <!-- Splash 覆蓋層 -->
    <LinearLayout android:id="@+id/splash_overlay"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:orientation="vertical" android:gravity="center"
        android:background="@color/white">
        <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:src="@drawable/splash_logo" android:layout_marginBottom="16dp"/>
        <TextView android:id="@+id/splash_text" android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="正在初始化..." android:textColor="@color/black"/>
    </LinearLayout>
</FrameLayout>

MainActivity.java

public class MainActivity extends Activity {
    private static final long SPLASH_DURATION = 1000;
    private LinearLayout splashOverlay;
    private TextView splashText;
    private Animation fadeIn, fadeOut;

    private final Handler splashHandler = new Handler(msg -> {
        if (msg.what == 0) {
            splashOverlay.startAnimation(fadeOut);
            splashOverlay.setVisibility(View.GONE);
        }
        return true;
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        splashOverlay = findViewById(R.id.splash_overlay);
        splashText = findViewById(R.id.splash_text);

        fadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in);  // alpha 0→1
        fadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out); // alpha 1→0

        splashOverlay.startAnimation(fadeIn);
        splashHandler.sendEmptyMessageDelayed(0, SPLASH_DURATION);

        // 這裡可並行進行資料初始化、網路請求等
    }
}

res/anim/fade_in.xml

<alpha xmlns:android="..." android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500"/>

res/anim/fade_out.xml

<alpha xmlns:android="..." android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500"/>

方案比較與建議

維度雙 Activity單 Activity View 切換
啟動速度稍慢(兩次 onCreate)快(單次 onCreate)
動畫流暢度可能有白閃無縫銜接,可做淡入淡出
初始化並行度Splash 期間可做初始化同樣可在 Handler 前啟動異步任務
程式碼複雜度低中(需管理 View 狀態)
適用場景簡單專案、舊版本相容追求體驗、現代應用首選

現代最佳實踐 (Android 12+): 使用 SplashScreen API (androidx.core:splashscreen),在 onCreate 前由系統自動顯示啟動圖示,支援動畫、圖示自適應、無需自定義 Activity,詳見官方文件 SplashScreen Compat Library。

copyright © 萬盛學電腦網 all rights reserved