1. The rendering

2. Custom properties

  • TextColor Specifies the font color
  • TextSize Specifies the font size
  • Duration Indicates the time displayed

3. Instructions

Step 1. Add it in your root build.gradle at the end of repositories:

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
Step 2. Add the dependency

	dependencies {
	        compile 'com. Making. WelliJohn: AnimTextView: 1.0.0'
	}
Copy the code
<wellijohn.org.animtv.AnimTextView
        android:id="@+id/atv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        app:textColor="@color/colorAccent" 
        app:textSize="20sp" 
        app:duration="5000"/>
Copy the code

When used directly in animtv.settext (222.09); If there are more than three decimal places behind the decimal point, it will be automatically rounded

4. The idea of implementation is very simple, an animation to solve the display of the number from 0 to traverse once, and then refresh the UI.

 ValueAnimator va;
        if (mIsInteger) {
            va = ValueAnimator.ofInt(0, (Integer) mEndText);
        } else {
            va = ValueAnimator.ofFloat(0, Float.parseFloat(String.valueOf(mEndText)));
        }

        va.setDuration(mDuration);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mDrawText = mIsInteger ? (T) animation.getAnimatedValue() : (T) df.format(animation.getAnimatedValue()); ViewCompat.postInvalidateOnAnimation(AnimTextView.this); }}); va.start();Copy the code

Note that the onMeasure method is overridden to support the padding and width of the wrap_content property. Here is a very simple one to practice on when you have time to do some scrolling up and down effects. Making the address