CountdownTimer不等1秒钟?

时间:2015-03-13 01:23:13

标签: android timer countdowntimer

我目前正在为Android开发一款游戏,在游戏中,有一个匹配的计时器,我用countDownTimer实现了它。问题是我的计时器正在减少,就像毫秒,我希望它是每秒钟。顺便说一下,我的平板电脑似乎很快,但如果这是问题,我不知道。我不知道代码有什么问题,但这里是:

this.timerFimDeJogo = new CountDownTimer(mSecondsLeft * 1000, 1000) {

        public void onTick(long millisUntilFinished) {
            --mSecondsLeft;
            if(mSecondsLeft == 10)
            {
                //pouco tempo para acabar? add animação no timer!
                final Animation animScale = AnimationUtils.loadAnimation(TelaModoCompeticao.this, R.anim.anim_scale_clock);
                TextView viewTimer = (TextView) findViewById(R.id.countdown);
                viewTimer.setTextColor(Color.RED);
                viewTimer.startAnimation(animScale);

            }
            String tempoAtual = String.format("%02d:%02d", 
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))
                );

            // update countdown
            ((TextView) findViewById(R.id.countdown)).setText(tempoAtual);
        }

        public void onFinish() {
            if(jogoJahTerminou == false)
            {
                // finish game
                mandarMensagemMultiplayer("terminouJogo;");
                ProgressDialog barraProgressoFinalTerminouJogo =  ProgressDialog.show(TelaModoCompeticao.this, getResources().getString(R.string.aviso_tempo_acaboou), getResources().getString(R.string.por_favor_aguarde));
                TerminaPartidaTask taskTerminaPartida = new TerminaPartidaTask(barraProgressoFinalTerminouJogo, TelaModoCompeticao.this);
                taskTerminaPartida.execute("");
            }

        }
     }.start();

1 个答案:

答案 0 :(得分:0)

如果我没错,试试这个......

    public class MyCount extends CountDownTimer {

    public MyCount(long millisUntilFinished, long countDownInterval) {
        super(millisUntilFinished, countDownInterval);
        onTick(millisUntilFinished);
    }

    public void onTick(long millisUntilFinished) {
        String time = formatTime(millisUntilFinished);
        Log.d("TIME","Remaining Time " + time);
    }

    public void onFinish() {
         Log.d("TIME","Time Over");
    }
}

// method for formatted time----------
public String formatTime(long millis) {
    String output = "00:00";
    long seconds = millis / 1000;
    long minutes = seconds / 60;

    seconds = seconds % 60;
    minutes = minutes % 60;

    String sec = String.valueOf(seconds);
    String min = String.valueOf(minutes);

    if (seconds < 10)
        sec = "0" + seconds;
    if (minutes < 10)
        min = "0" + minutes;

    output = min + " : " + sec;
    return output;
}

    MyCount count = new MyCount(selected_time * 60 * 1000, 1000);
     count.start();

希望它有所帮助:)