How to make this action happen at the end of the time when there are still 5 seconds left.
This is the code of the action that I want to do when there are 5 seconds left from the timer:
mTextViewCountDown.setTextColor(Color.RED);
zooming_second = AnimationUtils.loadAnimation(Level1.this, R.anim.watch_zoo);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
In this method:
//variable start
private static final long START_TIME_IN_MILLIS = 60000;
private TextView mTextViewCountDown;
private CountDownTimer mCountDownTimer;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
//variable end
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.ENGLISH, "d:d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
mTimerRunning = true;
}
@Override
public void onFinish() {
mTimerRunning = false;
TimeOutDialog();
soundPlay(time_out_sound);
mediaPlayer1.stop();
tadam_false.stop();
tadam_true.stop();
}
}.start();
}
private void pauseTimer () {
mCountDownTimer.cancel();
mTimerRunning = false;
}
CodePudding user response:
Inside onTick(long)
method simply check how many seconds are left. And if it is five, trigger your action.
EDIT
My approach to hitting the right time would be something like this:
if (Math.abs(mTimeLeftInMillis - 5000) < 100) {
// start the animation
}
This would avoid issues with if (mTimeLeftInMillis == 5000)
as it gives it some buffer. It's needed because it will not trigger at exactly 5000 milliseconds.
CodePudding user response:
Thanks! I did it like this:
if (mTimeLeftInMillis <= 6000){
mTextViewCountDown.setTextColor(Color.RED);
zooming_second = AnimationUtils.loadAnimation(Level1.this,
R.anim.watch_zoo);
mTextViewCountDown.startAnimation(zooming_second);
soundPlay(watch);
}