編輯:關於 Android 程式設計
余額寶那種金額數字從 0 快速滾動到最終值的動畫,視覺效果很好,實作起來卻不難。以下是一個可直接複用的自訂 TextView。

package com.lcw.rabbit.widget;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;
/**
* 高仿余額寶數字動畫
*/
public class CountNumberView extends TextView {
// 動畫時長
private int duration = 1500;
// 目前顯示的數字
private float number;
// 顯示格式
private String regex;
/** 不保留小數,整數 */
public static final String INTREGEX = "%1$01.0f";
/** 保留 2 位小數 */
public static final String FLOATREGEX = "%1$01.2f";
public CountNumberView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* 顯示帶有動畫效果的數字
*
* @param number 目標數值
* @param regex 顯示格式
*/
public void showNumberWithAnimation(float number, String regex) {
if (TextUtils.isEmpty(regex)) {
this.regex = INTREGEX; // 預設為整數
} else {
this.regex = regex;
}
// 修改 number 屬性時會觸發 setNumber
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "number", 0, number);
objectAnimator.setDuration(duration);
// 先加速再減速
objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
objectAnimator.start();
}
/** 取得目前數字 */
public float getNumber() {
return number;
}
/** 依格式顯示數字 */
public void setNumber(float number) {
this.number = number;
setText(String.format(regex, number));
}
}
在 XML 中像普通 TextView 一樣宣告,然後在 Java 中呼叫即可:
mTvCountNum1.showNumberWithAnimation(3201.23f, CountNumberView.FLOATREGEX); mTvCountNum2.showNumberWithAnimation(65535f, CountNumberView.INTREGEX);
為了保留彈性,程式預設提供了整數與浮點兩種格式,你也可以自行修改 regex 來符合需求。
格式說明符的完整語法如下:
%[argument_index$][flags][width][.precision]conversion
argument_index:十進位整數,標明參數在參數列表中的位置。第一個參數用 1$ 引用,第二個用 2$,依此類推。flags:修改輸出格式的字元集,可用集合依轉換類型而定。width:非負整數,表示寫入輸出的最少字元數。precision:非負整數,通常用來限制字元數,具體行為依轉換類型而定。整個動畫的核心其實只有一個技巧:透過 ObjectAnimator 讓 number 這個屬性從 0 平滑過渡到目標值,並在 setter 中即時把格式化後的文字設定到 TextView 上。加上 AccelerateDecelerateInterpolator 之後,數字的跳動就有了先快後慢的自然節奏。
這個模式可以延伸應用到任何需要「數值變化動畫」的場景,例如進度數字、統計儀表板或等級經驗值等。
Android - 查看應用(apk)簽名的方法
查看 Android 應用簽名在微博、微信開放平台注冊應用時,往往需要填寫應用(apk)的簽名。此時可以借助 keytool 工具來查詢一個已簽名應用的簽名信息。具體步驟
Android 性能優化實戰:布局層級精簡完全指南
布局是 Android 應用性能的第一道門檻。層級越深、節點越多,measure、layout、draw 三個階段的開銷就越大,頁面渲染與啟動速度也會被拖慢。本文從標籤抽
Android 4.4 KitKat Phone 工作流程淺析(七)——來電(MT)響鈴流程
本文來自 https://blog.csdn.net/yihongyuelan ,轉載請務必註明出處。本文程式碼以 MTK 平台 Android 4.4 為分析對象,與
Android Vibrator 震動模式控制教學
Android 的 Vibrator 服務可以讓應用程式觸發手機震動,常用於提示、遊戲回饋或通知效果。使用時必須先在 AndroidManifest.xml 加入震動權限