Android 仿餘額寶數字動畫效果

來源:互聯網
上載者:User

標籤:

看過好幾篇進行數字動畫處理的文章,這個是我發現的最簡單也是最好理解的,特地拿出來和大家分享。

核心的代碼主要是重寫了 TextView這個控制項,做了一些自己的處理。

package com.lance.widget;import java.text.DecimalFormat;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import android.content.Context;import android.os.Handler;import android.os.Message;import android.util.AttributeSet;import android.util.Log;import android.widget.TextView;/** * 一、設定跳躍的幀數,setFrames(int frames),預設是25幀 二、設定數字格式 , setFormat(String * pattern),具體查DecimalFormat類的api 三、需要動畫效果用,playNumber(double * number)方法代替setText()方法,小數如果超過3位小數會四捨五入保留2位小數 *  * @author Lance * */public class RunningTextView extends TextView {    public double content;// 最後顯示的數字    private int frames = 25;// 總共跳躍的幀數,預設25跳    private double nowNumber = 0.00;// 顯示的時間    private ExecutorService thread_pool;    private Handler handler;    private DecimalFormat formater;// 格式化時間,保留兩位小數    public RunningTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init();    }    public RunningTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init();    }    public RunningTextView(Context context) {        super(context);        init();    }    public int getFrames() {        return frames;    }    // 設定幀數    public void setFrames(int frames) {        this.frames = frames;    }    /**     * 設定數字格式,具體查DecimalFormat類的api     * @param pattern     */    public void setFormat(String pattern) {        formater = new DecimalFormat(pattern);    }    // 初始化    private void init() {        thread_pool = Executors.newFixedThreadPool(2);// 2個線程的線程池        formater = new DecimalFormat("00.00");// 最多兩位小數,而且不夠兩位整數用0佔位。可以通過setFormat再次設定   decimalformat用來格式化數字        handler = new Handler() {            @Override            public void handleMessage(Message msg) {                super.handleMessage(msg);                RunningTextView.this.setText(formater.format(nowNumber)                        .toString());// 更新顯示的數字                nowNumber += Double.parseDouble(msg.obj.toString());// 跳躍arg1那麼多的數字間隔//                Log.v("nowNumber增加之後的值", nowNumber + "");                //                if (nowNumber < content) {                    Message msg2 = handler.obtainMessage();                    msg2.obj = msg.obj;                    handler.sendMessage(msg2);// 繼續發送通知改變UI                } else {                    RunningTextView.this.setText(formater.format(content)                            .toString());// 最後顯示的數字,動畫停止                }            }        };    }    /**     * 播放數字動畫的方法     *     * @param moneyNumber     */    public void playNumber(double moneyNumber) {        if (moneyNumber == 0) {            RunningTextView.this.setText("0.00");            return;        }        content = moneyNumber;// 設定最後要顯示的數字        nowNumber = 0.00;// 預設都是從0開始動畫        thread_pool.execute(new Runnable() {            @Override            public void run() {                Message msg = handler.obtainMessage();                double temp = content / frames;                msg.obj = temp < 0.01 ? 0.01 : temp;// 如果每幀的間隔比1小,就設定為1//                Log.v("每幀跳躍的數量:", "" + msg.obj.toString());                handler.sendMessage(msg);// 發送通知改變UI            }        });    }}

 

  原理主要是利用handler進行遞迴 不斷的累加數字

package com.lance.runningtextview;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickListener {    private Button mBtrmb;    private Button mBtdollar;    private Button mBturo;    private com.lance.widget.RunningTextView mRunningtextview;    private EditText mEdit;    private Button mBtplay;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        bindViews();        // 設定數字格式,具體查DecimalFormat類的api        mRunningtextview.setFormat("00.00");        mBtplay.setOnClickListener(this);        mBtrmb.setOnClickListener(this);        mBtdollar.setOnClickListener(this);        mBturo.setOnClickListener(this);    }    private void bindViews() {        mBtrmb = (Button) findViewById(R.id.btrmb);        mBtdollar = (Button) findViewById(R.id.btdollar);        mBturo = (Button) findViewById(R.id.bturo);        mRunningtextview = (com.lance.widget.RunningTextView) findViewById(R.id.runningtextview);        mEdit = (EditText) findViewById(R.id.edit);        mBtplay = (Button) findViewById(R.id.btplay);    }    @Override    public void onClick(View v) {        switch (v.getId()) {        case R.id.btplay:            // 播放數字動畫            String temp = mEdit.getText().toString();            Log.v("temp:", temp);            if(!temp.equals("")){                double number = Double.parseDouble(temp);                mRunningtextview.playNumber(number);            }            break;        case R.id.btrmb:            Toast.makeText(this, "use the symbol ¥ ", Toast.LENGTH_SHORT).show();            mRunningtextview.setFormat("¥00.00");            break;        case R.id.bturo:            Toast.makeText(this, "use the symbol € ", Toast.LENGTH_SHORT).show();            mRunningtextview.setFormat("€00.00");            break;        case R.id.btdollar:            Toast.makeText(this, "use the symbol $ ", Toast.LENGTH_SHORT).show();            mRunningtextview.setFormat("$00.00");            break;        }    }}

 

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.