安卓游戏倒计时 [英] Android game countdown timer

查看:240
本文介绍了安卓游戏倒计时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个游戏,我需要一个倒数计时器。

I'm working on a game where I need a countdown timer.

我需要能够暂停计时器,重新开始倒计时,并在一定时间添加到当前倒计时状态。

I need to be able to pause the timer, resume the countdown and to add some time to current countdown state.

我看了 CountdownTimer 类及其方法,但似乎它不具备所需的功能。

I've looked at CountdownTimer class and its methods, but it seems like it doesn't have the required features.

我需要建议 - 这部分是最适合这个案子?

I need advice - which component is best for this case?

如何使用它呢?

可能是什么问题?

的AsyncTask 定时

有没有人有这样的经历?

Does anyone have experience with this?

推荐答案

我觉得线程可以使用,但其更容易实现使用CountdownTimer包装类的功能:

I think Thread can be used, but its easier to implement your features using CountdownTimer wrapper class:

    static class MyCountdownTimer {

    long mCurrentMilisLeft;
    long mInterval;
    CountdownTimerWrapper mCountdownTimer;

    class CountdownTimerWrapper extends CountDownTimer{
        public CountdownTimerWrapper(long millisInFuture,long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {

        }

        @Override
        public void onTick(long millisUntilFinished) {
            mCurrentMilisLeft = millisUntilFinished;
        }

    }

    public MyCountdownTimer(long millisInFuture, long countDownInterval) {          
        set(millisInFuture,countDownInterval);
    }

    public void pause(){
        mCountdownTimer.cancel();
    }

    public void resume(){
        mCountdownTimer = new CountdownTimerWrapper(mCurrentMilisLeft, mInterval);
        mCountdownTimer.start();
    }

    public void start(){
        mCountdownTimer.start();
    }

    public void set(long millisInFuture, long countDownInterval){
        mInterval = countDownInterval;
        mCurrentMilisLeft = millisInFuture;         
        mCountdownTimer = new CountdownTimerWrapper(millisInFuture, countDownInterval);
    }

}

这篇关于安卓游戏倒计时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆