简体   繁体   中英

How can I display countdown timer on the canvas in android?

I am making a simple android app in which i want to display countdown timer on canvas. I just want that when the game starts the countdown timer begins with 60 sec and decrements along..... Pls can someone suggests me how do i use the countdown timer class?

There's plenty of documentation available if you actually look for it:

// 60 second (60000ms) timer with ticks every second (1000ms)
new CountDownTimer(60000, 1000) {
    public void onTick(long millisUntilFinished) {
        mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
    }
    public void onFinish() {
        mTextField.setText("done!");
    }
}.start();

Now just replace the mTextField.setText() lines to something that would update your Canvas (or update the variable the Canvas uses for your time notification, then invalidate() the Canvas to display the update).

Even though I believe it's self-explanatory, when onFinish() is called, the CountDownTimer has reached 0 and is finished counting down.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM