Android 数字刷新从0开始变化的样式

在项目中用到的刷新数据的时候,从0开始刷新数据直到显示到最终的结果.

最终的效果图是这样子的,比较丑,就是个Demo,大家可以放在自己需要的界面上,
Android 数字刷新从0开始变化的样式_第1张图片

具体的实现代码如下:

package com.example.listviewtest;

import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
    private TextView one;
    private TextView two;
    private TextView three;
    private TextView four;

    private Timer timer = null;
    private TimerTask mTimerTaskt;
    private int mIndext = 0;
    public static int MI = 50;

    private Double mRealValue1 = (Double) 1000.00, mRealValue2 = (Double) 50000.00,
            mRealValue3 = (Double) 44332.00, mRealValue4 = (Double)5654.00;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        one = (TextView) findViewById(R.id.one);
        two = (TextView) findViewById(R.id.two);
        three = (TextView) findViewById(R.id.three);
        four = (TextView) findViewById(R.id.four);
    }

    @Override
    protected void onResume() {
        super.onResume();
        timer = new Timer(true);
        setView();
    }

    public void refresh(View v){
        mIndext = 0;
        setView();
    }

    private void setView(){
        move();
    }

    private void move() {
        if (mIndext != 10) {
            if (timer != null) {
                if (mTimerTaskt != null)
                    mTimerTaskt.cancel(); // 将原任务从队列中移除
                mTimerTaskt = new MyTimerTaskT(); // 新建
                timer.schedule(mTimerTaskt, 10, MI);
            }
        }
    }

    private class MyTimerTaskT extends TimerTask {

        public void run() {

            int cont = 3;
            if (mIndext < 10) {
                mIndext++;
                cont = 3;
            } else {
                cont = 0;
            }
            handlert.sendMessageDelayed(
                    handlert.obtainMessage(cont, mIndext + ""), MI);
        }
    };
    /**
     * DecimalFormat转换最简便
     */
    public static String m2D(Double f) {
        DecimalFormat df = new DecimalFormat("#,000.00");
        if (f < 1000) {
            df = new DecimalFormat("#0.00");
        } else if (f >= 1000 && f < 10000000) {
            df = new DecimalFormat("#0,000.00");
        } else if (f >= 10000000) {
            df = new DecimalFormat("#0,000,000.00");
        }
        return df.format(f);
    }
    Handler handlert = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 3) {
                mIndext = Integer.parseInt(String.valueOf(msg.obj));
                Double value1 = mRealValue1 * mIndext / 10;
                Double value2 = mRealValue2 * mIndext / 10;
                Double value3 = mRealValue3 * mIndext / 10;
                Double value4 = mRealValue4 * mIndext / 10;
                if (mIndext == 10) {
                    value2 = mRealValue2;
                    value3 = mRealValue3;
                    value1 = mRealValue1;
                    value4 = mRealValue4;
                }
                three.setText(m2D((value3)));
                two.setText(m2D((value2)));
                one.setText(m2D((value1)));
                four.setText(m2D((value4)));
                int currentapiVersion = android.os.Build.VERSION.SDK_INT;
                if (currentapiVersion >= 11) {
                    three.setAlpha((float) (mIndext == 10 ? 1 : mIndext == 0
                            || mIndext == 1 ? 0.5 : mIndext == 2
                            || mIndext == 3 ? 0.6 : mIndext == 4
                            || mIndext == 5 ? 0.7 : mIndext == 6
                            || mIndext == 7 ? 0.8 : 0.9));
                    one.setAlpha((float) (mIndext == 10 ? 1
                            : mIndext == 0 || mIndext == 1 ? 0.5 : mIndext == 2
                                    || mIndext == 3 ? 0.6 : mIndext == 4
                                    || mIndext == 5 ? 0.7 : mIndext == 6
                                    || mIndext == 7 ? 0.8 : 0.9));
                    two.setAlpha((float) (mIndext == 10 ? 1 : mIndext == 0
                            || mIndext == 1 ? 0.5 : mIndext == 2
                            || mIndext == 3 ? 0.6 : mIndext == 4
                            || mIndext == 5 ? 0.7 : mIndext == 6
                            || mIndext == 7 ? 0.8 : 0.9));
                    four.setAlpha((float) (mIndext == 10 ? 1 : mIndext == 0
                            || mIndext == 1 ? 0.5 : mIndext == 2
                            || mIndext == 3 ? 0.6 : mIndext == 4
                            || mIndext == 5 ? 0.7 : mIndext == 6
                            || mIndext == 7 ? 0.8 : 0.9));
                }
            }
        }
    };
}

也可以到这个地址去下载demo运行:
http://download.csdn.net/detail/zc1994113/9624918

你可能感兴趣的:(移动android开发,Android,数字刷新,从0开始)