Android仿支付寶中餘額寶的數字動畫效果_Android

來源:互聯網
上載者:User

實現效果圖:

下面是具體代碼,可直接複製:

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;/** * 高仿餘額寶數字動畫 * Create by: chenwei.li * Date: 2016-07-20 * time: 11:52 * Email: lichenwei.me@foxmail.com */public class CountNumberView extends TextView { //動畫時間長度 private int duration = 1500; //顯示數字 private float number; //顯示運算式 private String regex; //顯示表示式 public static final String INTREGEX = "%1$01.0f";//不保留小數,整數 public static final String FLOATREGEX = "%1$01.2f";//保留2位小數 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(); } /** * 擷取當前數字 * @return */ public float getNumber() { return number; } /** * 根據Regex,顯示對應數字樣式 * @param 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

關於String.format的第一個格式參數,這裡引用下api的描述:

常規類型、字元類型和數實值型別的格式說明符的文法如下:

%[argument_index$][flags][width][.precision]conversion

可選的 argument_index 是一個十進位整數,用於表明參數在參數列表中的位置。第一個參數由 "1$" 引用,第二個參數由 "2$" 引用,依此類推。

可選 flags 是修改輸出格式的字元集。有效標誌集取決於轉換類型。

可選 width 是一個非負十進位整數,表明要向輸出中寫入的最少字元數。

可選 precision 是一個非負十進位整數,通常用來限制字元數。特定行為取決於轉換類型。

總結

Android仿支付寶中餘額寶的數字動畫效果到這就結束了,希望這篇文章對大家在Android開發中能有所協助,如果有疑問可以留言交流。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.