继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

一个简单的滚动数字的效果实现

喵喔喔
关注TA
已关注
手记 554
粉丝 103
获赞 606

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属性的大小设置。这里是一个非常简单的,拿来练练手,等有时间做点上下滚动的特效。

原文链接:http://www.apkbus.com/blog-945380-76940.html

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP