androidtextviewobjectanimatoranimatorevaluator

ValueAnimator does not work how it should. Restarts from 0 instead of from 1st value


This is my code:

 public static void countTextViewTravelMode(final Integer value, final TextView myView, final CrudStateCallback back){
    Integer begin = 0;
    try {
        begin = Integer.parseInt(myView.getText().toString());
    }catch (Exception e){
        Log.e("","error trying to get value travel:" + e.getMessage());
    }
    ValueAnimator animator = ValueAnimator.ofInt(begin, value);
    animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        public void onAnimationUpdate(ValueAnimator animation) {
            String string = animation.getAnimatedValue() + "";
            myView.setText(string);
        }
    });
    animator.setEvaluator(new TypeEvaluator<Integer>() {
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            return Math.round((endValue - startValue) * fraction);
        }
    });
    animator.setDuration(1000);
    animator.start();
    if(back!= null)
        back.onResponse("");
}

This is being called once when the activity is called, and once after the Api call to get the new data is done. First time it goes from 0 to the correct value. Second time, it goes back to 0, starts from 0, and always stops at a random number, that is smaller than the wanted number. How can I make it not restart from 0, and also, finish at the wanted nr?


Solution

  • This was wrong:

      animator.setEvaluator(new TypeEvaluator<Integer>() {
        public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
            return Math.round((endValue - startValue) * fraction);
        } 
    }); 
    

    Took it out and it works like a charm