Android倒計時項目中的應用

這裏寫圖片描述

今天拿到UI圖,看到很多產品模塊中涉及到倒計時,本想偷偷懶在網上copy一份源碼,在覽閱中發現並不適合多個模塊管理,最後決定親自擼起。


UI圖
這裏寫圖片描述


源碼中主要分爲兩個類

  • CountdownManager類
    * 倒計時管理器,所有需要倒計時的頁面,都需要添加監聽接口,避免每個模塊單獨起線程,浪費時間。 在退出模塊時 必須調用unRegisterCountDownTimer方法,否則會造成內存泄露。*
private ArrayList<CountDownTimer> timers = new ArrayList<>();

    private static CountdownManager ourInstance = new CountdownManager();

    public static CountdownManager getInstance() {
        return ourInstance;
    }

    private CountdownManager() {

    }

    public void registerCountDownTimer(CountDownTimer countDownTimer) {
        if (countDownTimer.remainTime > 0) {
            timers.add(countDownTimer);
        }

        if (timers.size() == 1) {
            new Thread(this).start();
        }

    }

    public void unRegisterCountDownTimer(CountDownTimer countDownTimer) {
        if (countDownTimer.remainTime>1)
            countDownTimer.remainTime=1;

    }

    @Override
    public void run() {
        while (timers.size() > 0) {
            handler.sendEmptyMessage(0);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    ArrayList<CountDownTimer> completeTimes = new ArrayList<>();
    Handler handler;

    {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {


                super.handleMessage(msg);
                synchronized (this) {

                    for (CountDownTimer timer : timers) {
                        timer.remainTime--;

                        int second = (int) (timer.remainTime % 60);
                        int minute = (int) (timer.remainTime / 60 % 60);
                        int hour = (int) (timer.remainTime / 60 / 60);
                        timer.onTimeChange(hour, minute, second, timer.remainTime);
                        if (timer.remainTime <= 0) {
                            completeTimes.add(timer);
                        }

                    }
                    timers.removeAll(completeTimes);
                    completeTimes.clear();
                }

            }
        };
    }
主要操作和管理都放在這個類中

  • CountDownTimer接口
    作用:配合管理類時間變化進行回調
    public long remainTime;
    public TextView tv_hour,tv_minute,tv_second,textView;

    public abstract void onTimeChange(int hour, int minute, int second, long remainTime);

}
remainTime:設置的倒計時時間,第二列變量分別表示:時,分,秒,擴展的(view),第二列並不是必須使用,因項目而定

  • 如何快速遷移到項目中:
  • 複製以上兩個類到項目中
  • 註冊監聽,設置倒計時時間(秒)
  @OnClick(R.id.btn)
    public void onClicktime() {
        timer = new Timer();
        timer.remainTime = 3700;
        time_tv.setEnabled(false);
        CountdownManager.getInstance().registerCountDownTimer(timer);
        Log.v("registerCountDownTimer ", timer + "註冊時間");
    }
  • 實現CountDownTimer接口,回調中處理時間倒計時顯示
class Timer extends CountDownTimer {
        @Override
        public void onTimeChange(int hour, int minute, int second, long remainTime) {
            if (remainTime <= 0) {

                time_tv.setText("重新發送驗證碼");
                time_tv.setEnabled(true);
                if (timer != null) {
                      CountdownManager.getInstance().unRegisterCountDownTimer(timer);
                    timer = null;
                }
                Log.v("registerCountDownTimer ", hour + "===時1" + minute + "===分" + second + "====秒" + timer);
            } else {
                Log.v("registerCountDownTimer ", hour + "===時" + minute + "===分" + second + "====秒" + timer);
                time_tv.setText("倒計時(h:m:s)格式:" + hour + ":" + minute + ":" + second + "   ,S(格式):" + remainTime);
            }
        }
    }
  • 重要一點:退出進行銷燬
 @Override
    public void finish() {
        if (timer != null) {
            Log.v("registerCountDownTimer", timer + "銷燬時間");
            CountdownManager.getInstance().unRegisterCountDownTimer(timer);
            timer = null;
        }
        super.finish();
    }

想get更多優質源碼,關注專屬開發者公衆號:開發者源碼。
所有的源碼都在這裏~

這裏寫圖片描述


源碼下載地址:http://download.csdn.net/detail/qq_15895655/9780708

解壓密碼:0912 ,100%親測可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章