本案例是使用開源組件RiseNumberTextView來實現類似手機支付寶中增長的數字效果,首先我們來看一下開源組件中的兩個類分別如下:
package com.bear.risenumbertest.lib;
public interface RiseNumberBase {
public void start();
public RiseNumberTextView withNumber(float number);
public RiseNumberTextView withNumber(int number);
public RiseNumberTextView setDuration(long duration);
public void setOnEnd(RiseNumberTextView.EndListener callback);
}
另一個類:
package com.bear.risenumbertest.lib;
import Android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
import com.nineoldandroids.animation.ValueAnimator;
import java.text.DecimalFormat;
public class RiseNumberTextView extends TextView implements RiseNumberBase{
private static final int STOPPED = 0;
private static final int RUNNING = 1;
private int mPlayingState = STOPPED;
private float number;
private float fromNumber;
private long duration=1500;
/**
* 1.int 2.float
*/
private int numberType=2;
private DecimalFormat fnum;
private EndListener mEndListener=null;
final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
public RiseNumberTextView(Context context){
super(context);
}
public RiseNumberTextView(Context context,AttributeSet attr){
super(context,attr);
}
public RiseNumberTextView(Context context,AttributeSet attr,int defStyle)
{
super(context,attr,defStyle);
}
public interface EndListener {
public void onEndFinish();
}
public boolean isRunning() {
return (mPlayingState == RUNNING);
}
private void runFloat(){
ValueAnimator valueAnimator = ValueAnimator.ofFloat(fromNumber, number);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setText(fnum.format(Float.parseFloat(valueAnimator.getAnimatedValue().toString())));
if (valueAnimator.getAnimatedFraction()>=1){
mPlayingState = STOPPED;
if (mEndListener!=null)
mEndListener.onEndFinish();
}
}
});
valueAnimator.start();
}
private void runInt(){
ValueAnimator valueAnimator = ValueAnimator.ofInt((int)fromNumber, (int)number);
valueAnimator.setDuration(duration);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setText(valueAnimator.getAnimatedValue().toString());
if (valueAnimator.getAnimatedFraction()>=1){
mPlayingState = STOPPED;
if (mEndListener!=null)
mEndListener.onEndFinish();
}
}
});
valueAnimator.start();
}
static int sizeOfInt(int x) {
for (int i = 0;; i++)
if (x <= sizeTable[i])
return i + 1;
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
fnum= new DecimalFormat("##0.00");
}
@Override
public void start() {
if (!isRunning()) {
mPlayingState = RUNNING;
if (numberType==1)
runInt();
else
runFloat();
}
}
@Override
public RiseNumberTextView withNumber(float number) {
this.number=number;
numberType=2;
if (number>1000){
fromNumber=number-(float)Math.pow(10,sizeOfInt((int)number)-2);
}else {
fromNumber=number/2;
}
return this;
}
@Override
public RiseNumberTextView withNumber(int number) {
this.number=number;
numberType=1;
if (number>1000){
fromNumber=number-(float)Math.pow(10,sizeOfInt((int)number)-2);
}else {
fromNumber=number/2;
}
return this;
}
@Override
public RiseNumberTextView setDuration(long duration) {
this.duration=duration;
return this;
}
@Override
public void setOnEnd(EndListener callback) {
mEndListener=callback;
}
}
將這兩個類集成到我們的工程中後,就可以使用了,下面這個是使用范例:
package com.bear.risenumbertest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;
import com.bear.risenumbertest.lib.RiseNumberTextView;
import com.bear.risenumbertest.lib.RiseNumberTextView.EndListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲取到RiseNumberTextView對象
RiseNumberTextView rnTextView = (RiseNumberTextView) findViewById(R.id.risenumber_textview);
// 設置數據
rnTextView.withNumber(2666.50f);
// 設置動畫播放時間
rnTextView.setDuration(5000);
// 開始播放動畫
rnTextView.start();
// 監聽動畫播放結束
rnTextView.setOnEnd(new EndListener() {
@Override
public void onEndFinish() {
Toast.makeText(MainActivity.this, "數據增長完畢...",
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
附上效果圖:
最後附上Android之增長的數字動畫完整的工程源碼鏈接:
------------------------------------------分割線------------------------------------------
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2014年資料/12月/15日/Android之增長的數字
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11