How to set timer in android?

后端 未结 21 1217
渐次进展
渐次进展 2020-11-22 00:51

Can someone give a simple example of updating a textfield every second or so?

I want to make a flying ball and need to calculate/update the ball coordinates every se

21条回答
  •  野的像风
    2020-11-22 01:29

    You can also use an animator for it:

    int secondsToRun = 999;
    
    ValueAnimator timer = ValueAnimator.ofInt(secondsToRun);
    timer.setDuration(secondsToRun * 1000).setInterpolator(new LinearInterpolator());
    timer.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                int elapsedSeconds = (int) animation.getAnimatedValue();
                int minutes = elapsedSeconds / 60;
                int seconds = elapsedSeconds % 60;
    
                textView.setText(String.format("%d:%02d", minutes, seconds));
            }
        });
    timer.start();
    

提交回复
热议问题