Android中使用Handler以及CountDownTimer實現包含倒計時的閃屏頁面


上一篇博文《Android中Handler使用淺析》通過實現倒計時閃屏頁面的制作引出了Handler的使用方法以及實現原理,博文末尾也提到了實現過程中的Bug,有興趣的朋友可以點擊鏈接回去看看。今天通過使用Handler以及CountDownTimer來實現完整版的倒計時閃屏(不會出現在退出閃屏頁后,依然會跳轉頁面的現象)。

1. 實現效果如下:

1.1  正常進入跳轉的效果以及log顯示

1.2  倒計時未結束時退出以及log顯示

對比上篇博文的實現,退出后計時停止且不會再跳到新的界面

2. 實現方法

2.1 去除actionBar

閃屏頁面一般都為全屏顯示,這里我們首先需要去除actionBar,在res/values/styles.xml中設置:
這里也建議大家在后期開發中盡量不要用死板的actionBar,可以根據項目需求使用ToolBar或者自定義TitleBar組件來替代actionBar,這樣的話界面設計會更加靈活。

2.2 layout布局

這里僅僅設置布局背景圖片,以及在右上角添加TextView用於顯示倒計時,做的有點糙,見諒,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_splash"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background_login"
    tools:context="com.mly.panhouye.handlerdemo.SplashActivity">
    <TextView
        android:gravity="right"
        android:id="@+id/tv_time"
        android:textColor="@color/colorAccent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SS"
        android:textSize="30sp"/>
</RelativeLayout>

2.3 java實現代碼

2.1中只是去除了app的ActionBar,要做的全屏顯示,仍需要在activity中使用代碼設置。
package com.mly.panhouye.handlerdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

public class SplashActivity extends AppCompatActivity {
    private MyHandler myHandler = new MyHandler();
    private TextView tv_time;
    private MyCountDownTimer mc;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設置Activity為全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //去標題狀態欄
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        tv_time = (TextView) findViewById(R.id.tv_time);
        mc = new MyCountDownTimer(5000, 1000);
        mc.start();
        /**
         * 使用handler的postDelayed延遲5秒執行頁面跳轉
         * (與CountDownTimer的millisInFuture一致)
         */
        myHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startMainActivity();
            }
        },5000);
    }
    //將Handler聲明為靜態內部類
    private static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    }
    //頁面跳轉的方法
    private void startMainActivity(){
        Intent intent = new Intent(this,Main3Activity.class);
        startActivity(intent);
        finish();//完成跳轉后銷毀閃屏頁(從棧內移除)
    }
    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture
         *      表示以毫秒為單位 倒計時的總數
         *      例如 millisInFuture=1000 表示1秒
         * @param countDownInterval
         *      表示 間隔 多少微秒 調用一次 onTick 方法
         *      例如: countDownInterval =1000 ; 表示每1000毫秒調用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        public void onFinish() {
            tv_time.setText("正在跳轉");
        }

        public void onTick(long millisUntilFinished) {
            tv_time.setText("倒計時(" + millisUntilFinished / 1000 + ")");
            Log.i("tag","倒計時"+millisUntilFinished / 1000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //閃屏頁銷毀時將消息對象從消息隊列移除並結束倒計時
        myHandler.removeCallbacksAndMessages(null);
        mc.cancel();
        Log.i("tag","destory");
    }
}

到這里就全部實現了,實現過程就這么簡單,大家需要注意的是Handler內存泄漏的問題,具體請看

http://www.cnblogs.com/panhouye/p/6494753.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM