I'm developing a game. Usually in games there is a small clock that counts a few seconds to the contrary, type 10 to 0, and I would like to put one in my game.
How can I do this?
PS: I'm using Android Studio
I'm developing a game. Usually in games there is a small clock that counts a few seconds to the contrary, type 10 to 0, and I would like to put one in my game.
How can I do this?
PS: I'm using Android Studio
Use the class CountDownTimer .
new CountDownTimer(tempo, 1000) {
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
String hms = (TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis))) + ":" + (TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
// o código abaixo mostra quantos segundos faltam em uma textview
suaTextView.setTitle(hms);
tempo = millis;
}
public void onFinish() {
suaTextview.setTitle("00:00");
// executar uma ação quando o tempo acabar
}
}.start();