The effect of a simple rolling number

Posted by r3drain on Thu, 30 Apr 2020 15:14:46 +0200

1. Renderings

2. Custom attributes

  • textColor font color
  • textSize font size
  • Time displayed in duration text

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.github.WelliJohn:AnimTextView:1.0.0'
    }
<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"/>

When using, you can use it directly in animTv.setText(222.09); it is OK. Here, you need to note that the value only supports integer and decimal display, and the decimal only supports two decimal places. If there are more than three decimal places after the decimal point, it will be automatically rounded

4. The idea of implementation is very simple. An animation is solved. The number displayed is traversed from 0 to once, and then the ui is refreshed.

 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();

In addition, it should be noted that the onMeasure method is overridden to support padding, and the width is set to the size of the wrap content property. Here is a very simple, to practice hands, waiting for time to do point up and down rolling effect.
github address , your praise and star are the biggest driving force for me to continue to open source.

Topics: Android github Gradle Maven