一個簡單的滾動數字的效果實現

1.效果圖

2.定製的屬性

  • textColor 字體顏色
  • textSize 字體大小
  • duration 文字顯示出來的時間

3.使用說明

implementation 'wellijohn.org.tvanim:animtv:1.0.0'android

<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"/>

使用的時候,直接在animTv.setText(222.09);就能夠了,在這裏須要注意的是,這裏的數值只支持整型和小數顯示,小數只支持到小數點後兩個位,若是有小數點後有3位以上,自動四捨五入git

4.實現的思路很簡單,一個動畫就解決了,對顯示的數字從0到遍歷一次,而後刷新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();

另外須要注意的是重寫onMeasure方法,支持padding,width爲wrap_content屬性的大小設置。這裏是一個很是簡單的,拿來練練手,等有時間作點上下滾動的特效。

github地址github

相關文章
相關標籤/搜索