Android dynamically sets the TextView value. For example, the amount increases.

Source: Internet
Author: User

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!





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.