功能:仿余额宝动态数字显示收益
效果图:
主要实现代码:
package com.example.zzatest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
public class MainActivity extends Activity {
private MagicTextView mIncomeTxt;
private Button button1;//动态显示值
private Button button2;//归0
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
setContentView(R.layout.activity_main);
mIncomeTxt = (MagicTextView) findViewById(R.id.income_money);
button1=(Button) findViewById(R.id.button1);
button2=(Button) findViewById(R.id.button2);
mIncomeTxt.setValue(20,30);// mRate = (double)(20/50);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIncomeTxt.beginShowValue();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mIncomeTxt.BackZero();
}
});
}
}
package com.example.zzatest;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.widget.TextView;
public class MagicTextView extends TextView {
// 递减/递增 的变量值
private double mRate;
// view 设置的值
private double mValue;
// 当前显示的值
private double mCurValue;
// 当前变化后最终状态的目标值
private double mGalValue;
// 控制加减法
private int rate = 1;
// 偏移量 主要用来进行校正距离。
DecimalFormat fnum = new DecimalFormat("0.00");
public MagicTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0:
if (rate * mCurValue < mGalValue) {
setText(fnum.format(mCurValue));
mCurValue += mRate * rate;
mHandler.sendEmptyMessageDelayed(0, 50);
} else {
setText(fnum.format(mGalValue));
}
break;
default:
break;
}
};
};
/**
* 设置要显示的值
* @param value
*/
public void setValue(double value,int size) {
mValue = value;
//设定递增或递减比例
mRate = (double) (mValue / size);
BigDecimal b = new BigDecimal(mRate);
mRate = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 动态的展现设定值
*/
public void beginShowValue(){
rate = 1;//表示增加
//mCurValue = 0;
mGalValue = mValue;
mHandler.sendEmptyMessage(0);
}
/**
* 动态归0
*/
public void BackZero(){
rate = -1;//表示减少
//mCurValue=mValue;
mGalValue = 0;
mHandler.sendEmptyMessage(0);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6633"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="昨日收益"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<com.example.zzatest.MagicTextView
android:id="@+id/income_money"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="0.00"
android:textColor="#FFFFFF"
android:textSize="60sp" />
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:text="开始变化" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="开始归0" />
</RelativeLayout>