Search code examples
javaandroidtextviewandroid-animationanimator

ValueAnimator for count up effect on Android TextView


I am trying to make a cout-up effect in a TextView for a double value like this:

ValueAnimator animator = new  ValueAnimator();
animator.setObjectValues(0, 50.5); //double value
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    public void onAnimationUpdate(ValueAnimator animation) {
                textView.setText(String.valueOf(animation.getAnimatedValue()));
            }
        });
animator.setEvaluator(new TypeEvaluator<Double>() { // problem here
   @Override
   public Double evaluate(float fraction, Double startValue, Double endValue) {
      return  (startValue + (endValue - startValue) * fraction);
           }
        });
animator.setDuration(3000);
animator.start();// problem here

It gave me this: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double at the line animator.setEvaluator(new TypeEvaluator() at animator.start();

If I use the count up for a integer and implement The TypeEvaluator method, it works. Why not for a double?


Solution

  • animator.setObjectValues(0, 50.5); in this line 0 consider as int value

    change this line to

    animator.setObjectValues(0d, 50.5d); //double value