Android dynamically sets the TextView value. For example, the amount increases.
When it comes to setting the TextView value in a dynamic increments, many people should immediately think of a thread, set the time for sleep in the later thread, and use handler to send messages to update the TextView value!
In this way, the TextView value can be set dynamically and incrementally, but the efficiency is not very good. If you need it, try it by yourself. For example, if the value is displayed within 1 to 100 and 10 s, you will feel a bit stuck!
There is a better way to use ValueAnimator for setting, and you do not need to calculate the time interval after each superposition. The following code:
public static void autoIncrement(final TextView target, final float start,final float end, long duration) {ValueAnimator animator = ValueAnimator.ofFloat(start, end);animator.addUpdateListener(new AnimatorUpdateListener() {private FloatEvaluator evalutor = new FloatEvaluator();private DecimalFormat format = new DecimalFormat("####0.0#");@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float fraction = animation.getAnimatedFraction();float currentValue = evalutor.evaluate(fraction, start, end);target.setText(format.format(currentValue));}});animator.setDuration(duration);animator.start();}
1-values are displayed within 2 S, and the display is very smooth. If you do not believe it, try it on your own!