1.效果图
2.定制的属性
textColor 字体颜色
textSize 字体大小
duration 文字显示出来的时间
3.使用说明
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"/>
使用的时候,直接在animTv.setText(222.09);就可以了,在这里需要注意的是,这里的数值只支持整型和小数显示,小数只支持到小数点后两个位,如果有小数点后有3位以上,自动四舍五入
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属性的大小设置。这里是一个非常简单的,拿来练练手,等有时间做点上下滚动的特效。