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

Android 閃屏頁兩種做法:Handler 延遲跳轉與 CountDownTimer 倒計時

編輯:關於 Android 開發

Android 閃屏頁兩種做法:Handler 延遲跳轉與 CountDownTimer 倒計時

閃屏頁的實現其實很簡單,核心就是「延遲一會兒再跳轉」。這裡記錄兩種常見寫法:一種是純延遲,另一種帶可見的倒計時。

做法一:Handler.postDelayed 延遲跳轉

postDelayed() 接受兩個參數:一個 Runnable,裡面放延遲後要執行的操作;一個延遲時間,單位是毫秒。順手給 logo 加一個放大動畫,觀感會好不少。

package com.msn.zn.splashdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class SplashActivity extends Activity {

    private ImageView iv_logo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 取消標題
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 取消狀態欄
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        iv_logo = (ImageView) findViewById(R.id.logo);

        // 加載 xml 文件中定義的動畫
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.splash);
        iv_logo.startAnimation(anim);

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }, 3000);
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<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="com.msn.zn.splashdemo.SplashActivity">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/splasht2"/>

</RelativeLayout>

動畫定義:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:duration="1500"
    android:fillAfter="true">

    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.1"
        android:toYScale="1.1"/>

    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.8"/>

</set>

兩個屬性的作用:duration="1500" 把動畫時長設為 1.5 秒;fillAfter="true" 讓動畫結束後保持在最終狀態,不會跳回起始位置。這兩項在代碼裡也能設置:animation.setDuration(1500)、animation.setFillAfter(true)。

做法二:CountDownTimer 顯示倒計時

如果需要在界面上顯示「3 秒後跳轉」這類倒數,用系統封裝好的 CountDownTimer 最省事。它本質上是把後台線程的創建和 Handler 隊列封裝成了一個好用的類。

package com.msn.zn.splashdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.TextView;

public class SplashTimeActivity extends Activity {

    private TextView tv_time;
    private MyCountDownTimer mc;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_time);
        tv_time = (TextView) findViewById(R.id.tv_time);

        mc = new MyCountDownTimer(5000, 1000);
        mc.start(); // 啟動倒計時

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashTimeActivity.this, MainActivity.class));
            }
        }, 5000);
    }

    /**
     * 定義一個倒計時的內部類
     */
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture     從調用 start() 到倒計時完成、onFinish() 被調用的毫秒數
         * @param countDownInterval  接收 onTick(long) 回調的間隔時間
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            tv_time.setText("倒計時 " + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            tv_time.setText("正在跳轉");
        }
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<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:background="@mipmap/splasht2"
    tools:context="com.msn.zn.splashdemo.SplashTimeActivity">

    <TextView
        android:id="@+id/tv_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="20sp"
        android:text="閃一下" />

</RelativeLayout>

兩者怎麼選

純展示、不需要跟用戶交互的閃屏頁,做法一就够了。若還要提供「跳過」按鈕,倒計時就更合適——把跳轉邏輯收進 onFinish(),跳過按鈕則調用 mc.cancel() 後手動跳轉,兩條路徑不會打架。

另外提醒一個實務細節:Activity 銷毀時記得在 onDestroy() 裡調用 handler.removeCallbacksAndMessages(null) 並取消倒計時,否則用戶在 3 秒內按返回鍵退出,延遲任務仍會執行,可能把不該啟動的 Activity 拉起來。

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