dimanche 9 septembre 2018

Android - How to animate random numbers?

I want to make a random number from 0-9 with animation. I try to use with ValueAnimator, but no luck.

Here is my code without ValueAnimator (works):

final int min = 0;
final int max = 9;
final int random = new Random().nextInt((max - min) + 1) + min;
randomSting = String.valueOf(random);
mNumberView.setText(randomSting);

Here is with ValueAnimator that doesn't work (not works):

animator = ValueAnimator.ofInt(0, 9);
    final int min = 0;
    final int max = 9;
    final int random = new Random().nextInt((max - min) + 1) + min;
    randomSting = String.valueOf(random);
    cTimer = new CountDownTimer(3000, 1000) {
        public void onTick(long millisUntilFinished) {

            animator.setDuration(3000); 
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    mNumberView.setText(animation.getAnimatedValue().toString());
                }
            });
            animator.start();
        }

        public void onFinish() {
            animator.cancel();
            mNumberView.setText(randomSting);
        }
    };
    cTimer.start();

mNumberView cannot set text to randomString.

I hope can make number animator with random numbers, not using ValueAnimator.

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire