简体   繁体   English

如何在Android的画布上显示倒数计时器?

[英]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. 我正在制作一个简单的android应用,我想在画布上显示倒数计时器。 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? 我只想在游戏开始时倒数计时器以60秒开始并递减.....有人可以建议我如何使用倒数计时器类吗?

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). 现在,只需将mTextField.setText()行替换为将更新Canvas的内容(或更新Canvas用于时间通知的变量,然后使Canvas无效()以显示更新)。

Even though I believe it's self-explanatory, when onFinish() is called, the CountDownTimer has reached 0 and is finished counting down. 即使我认为这是不言自明的,但在调用onFinish()时,CountDownTimer已达到0并完成了递减计数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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