im trying to implement timer using android, such that it will countdown from given number to 0, to do it i use CountDownTimer... the problem is that i want it to be updated every 1 millisecond and it seems that the textView that i use to present the time is to slow when i try to setText... for example if i start the timer from 20000 and se 1 millisecond as countDownInterval the textView stops changing when it gets to approx 19000..

here is my code:

currTimer=new CountDownTimer(20000,10) {

            @Override
            public void onTick(long millisUntilFinished) {
                scoreBoard.setCurrMilliTime(scoreBoard.getCurrMilliTime()-10);
                textView.setText(Integer.toString(scoreBoard.getCurrMilliTime()));

            }

            @Override
            public void onFinish() {
                scoreBoard.setCurrMilliTime(scoreBoard.getCurrMilliTime()-10);
                textView.setText(Integer.toString(scoreBoard.getCurrMilliTime()));

            }
        }.start();
有帮助吗?

解决方案

I just tried this :

final TextView textView1 = (TextView) findViewById(R.id.textView1);
    new CountDownTimer(20000,10) {

        @Override
        public void onTick(long millisUntilFinished) {
            textView1.setText(String.format("%d : %d :%d", 
                    TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),

                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                    TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)), 

                    TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - 
                    TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - 
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))
                ));

        }

        @Override
        public void onFinish() {

        }
    }.start();

and works perfectly, hope this helps, although i don't think a step by "10" is average enough, try 100

其他提示

Use an animation, which is designed to change something on the screen a tiny bit every frame, which is perfect for what you're doing. Sample blatantly plagarized from https://stackoverflow.com/a/24389011/845092

    ValueAnimator animator = new ValueAnimator();
    animator.setObjectValues(0, count);
    animator.addUpdateListener(new AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            view.setText(String.valueOf(animation.getAnimatedValue()));
        }
    });
    animator.setEvaluator(new TypeEvaluator<Integer>() {
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            return Math.round(startValue + (endValue - startValue) * fraction);
        }
    });
    animator.setDuration(1000);
    animator.start();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top