NumRunningTextView改良版
在我曾寫過的一篇博客:http://blog.csdn.net/chay_chan/article/details/70196478 中,介紹了我自己封裝的一款仿支付寶數(shù)字滾動的TextView,有不少的朋友在評論中跟我提出建議,建議我使用ValueAnimator實現(xiàn)數(shù)字逐漸變化的功能,一開始在寫這個控件的時候,一時間沒有想到可以使用ValueAnimator來實現(xiàn)數(shù)字的遞增,當看到評論里熱心朋友的提醒后,覺得需要對這個控件進行改良,使用ValueAnimator來代替之前的一大堆操作,之前是通過使用handler實現(xiàn)遞歸,逐漸讓文字變化,比較麻煩,而改成使用ValueAnimator后,一切變得簡單了。
使用ValueAnimator.ofFloat()實現(xiàn)金額數(shù)字的變化
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)));//格式化成兩位小數(shù)
// 更新顯示的內(nèi)容
if (useCommaFormat) {
//使用每三位數(shù)字一個逗號的格式
String formatStr = StringUtils.addComma(str);//三位一個逗號格式的字符串
setText(formatStr);
} else {
setText(str);
}
}
});
floatAnimator.start();
使用ValueAnimator.ofInt()實現(xiàn)整型數(shù)字的變化
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();
新增功能
可以修改數(shù)字滾動動畫執(zhí)行的時間
如果不設(shè)置動畫執(zhí)行的周期,則會使用默認的動畫執(zhí)行周期,如下所示都是使用默認動畫執(zhí)行時間
<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"
/>
執(zhí)行的效果如下:

修改其中一個控件的動畫執(zhí)行時間:
<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"
/>
執(zhí)行的效果如下:

在使用的過程中,就可以通過改變動畫執(zhí)行的周期來控制數(shù)字滾動的速度了,只需在布局文件中,配置duration屬性,注意這里是以毫秒(ms)為單位。
可以修改數(shù)字最少要達到的某個值才會滾動
這個功能彌補之前空間的一個缺陷,就是在數(shù)字很小的時候,比如金額的數(shù)字為0.01,整型數(shù)字為1,那么動畫執(zhí)行的結(jié)果讓人感覺起來有點卡頓的感覺,如下所示:

所以需要讓數(shù)字達到某個值才可以進行滾動,當值未能到達這個值的時候,則不會滾動,當達到指定的值后,就可以進行滾動,對應(yīng)的屬性分別為minMoney(設(shè)置最小達到的金額)、minNum(設(shè)置最小達到的數(shù)字),使用如下:
<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)的時候,則不會執(zhí)行滾動的動畫,當我傳入的數(shù)字小于5的時候,數(shù)字也不會滾動。如圖所示

當數(shù)字達到要求的時候,則會滾動,如圖所示

如果不設(shè)置這個屬性,默認情況下,金額需要達到0.1,數(shù)字需要達到3才會進行滾動,具體需要可以根據(jù)使用進行配置,如果你不覺得數(shù)字過小時動畫看起來卡的話,那么可以設(shè)置這個屬性為0,如果是使用金錢類型,設(shè)置最小金額minMoney(浮點類型),如果是整型數(shù)字類型,設(shè)置最小的數(shù)字minNum(整數(shù)類型)。
導(dǎo)入方式####
在項目根目錄下的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