第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

NumberRunningTextView(數(shù)字會(huì)滾動(dòng)的TextView)

標(biāo)簽:
Android

NumRunningTextView改良版

  在我曾写过的一篇博客:http://blog.csdn.net/chay_chan/article/details/70196478 中,介绍了我自己封装的一款仿支付宝数字滚动的TextView,有不少的朋友在评论中跟我提出建议,建议我使用ValueAnimator实现数字逐渐变化的功能,一开始在写这个控件的时候,一时间没有想到可以使用ValueAnimator来实现数字的递增,当看到评论里热心朋友的提醒后,觉得需要对这个控件进行改良,使用ValueAnimator来代替之前的一大堆操作,之前是通过使用handler实现递归,逐渐让文字变化,比较麻烦,而改成使用ValueAnimator后,一切变得简单了。

使用ValueAnimator.ofFloat()实现金额数字的变化

     ValueAnimator floatAnimator =  ValueAnimator.ofFloat(0, finalFloat);
            floatAnimator.setDuration(duration);
            floatAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float currentNum = (float) animation.getAnimatedValue();
                    String str = formatter.format(Double.parseDouble(String.valueOf(currentNum)));//格式化成两位小数
                    // 更新显示的内容
                    if (useCommaFormat) {
                        //使用每三位数字一个逗号的格式
                        String formatStr = StringUtils.addComma(str);//三位一个逗号格式的字符串
                        setText(formatStr);
                    } else {
                        setText(str);
                    }
                }
            });
            floatAnimator.start();

使用ValueAnimator.ofInt()实现整型数字的变化

    ValueAnimator intAnimator = new ValueAnimator().ofInt(0, finalNum);
            intAnimator.setDuration(duration);
            intAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int currentNum = (int) animation.getAnimatedValue();
                    setText(String.valueOf(currentNum));
                }
            });
            intAnimator.start();

新增功能

可以修改数字滚动动画执行的时间

如果不设置动画执行的周期,则会使用默认的动画执行周期,如下所示都是使用默认动画执行时间

            <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_money"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="0.00"
                android:textColor="#fff"
                android:textSize="30sp"
                android:textStyle="bold"
                />

            <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="200"
                android:textColor="#fff"
                android:textSize="30sp"
                app:textType="num"
                />

执行的效果如下:

修改其中一个控件的动画执行时间:

    <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_money"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="0.00"
                android:textColor="#fff"
                android:textSize="30sp"
                android:textStyle="bold"
                />

            <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="200"
                android:textColor="#fff"
                android:textSize="30sp"
                app:textType="num"
                app:duration="3000" 
                />

执行的效果如下:

  在使用的过程中,就可以通过改变动画执行的周期来控制数字滚动的速度了,只需在布局文件中,配置duration属性,注意这里是以毫秒(ms)为单位。

可以修改数字最少要达到的某个值才会滚动

  这个功能弥补之前空间的一个缺陷,就是在数字很小的时候,比如金额的数字为0.01,整型数字为1,那么动画执行的结果让人感觉起来有点卡顿的感觉,如下所示:

  所以需要让数字达到某个值才可以进行滚动,当值未能到达这个值的时候,则不会滚动,当达到指定的值后,就可以进行滚动,对应的属性分别为minMoney(设置最小达到的金额)、minNum(设置最小达到的数字),使用如下:

        <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_money"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="0.00"
                android:textColor="#fff"
                android:textSize="30sp"
                android:textStyle="bold"
                app:runWhenChange="false"
                app:minMoney="0.98"
                />

         <com.chaychan.viewlib.NumberRunningTextView
                android:id="@+id/tv_num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="200"
                android:textColor="#fff"
                android:textSize="30sp"
                app:textType="num"
                app:runWhenChange="false"
                app:minNum="5"
                />

当我传入金额小于九毛八(0.98)的时候,则不会执行滚动的动画,当我传入的数字小于5的时候,数字也不会滚动。如图所示

当数字达到要求的时候,则会滚动,如图所示

  如果不设置这个属性,默认情况下,金额需要达到0.1,数字需要达到3才会进行滚动,具体需要可以根据使用进行配置,如果你不觉得数字过小时动画看起来卡的话,那么可以设置这个属性为0,如果是使用金钱类型,设置最小金额minMoney(浮点类型),如果是整型数字类型,设置最小的数字minNum(整数类型)。

导入方式

在项目根目录下的build.gradle中的allprojects{}中,添加jitpack仓库地址,如下:

    allprojects {
        repositories {
            jcenter()
            maven { url 'https://jitpack.io' }//添加jitpack仓库地址
        }
    }

打开app的module中的build.gradle,在dependencies{}中,添加依赖,如下:

    dependencies {
            ......
            compile 'com.github.chaychan:PowerfulViewLibrary:1.1.7'
    }

源码github地址:https://github.com/chaychan/PowerfulViewLibrary.git

點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
移動(dòng)開發(fā)工程師
手記
粉絲
62
獲贊與收藏
487

關(guān)注作者,訂閱最新文章

閱讀免費(fèi)教程

  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消