animationTextView增加整数,停止在某个点?

我有一个TextView显示整数值。 整数值是从以前的活动转移,我想添加漂亮的animation。 我想要如果例如int值是73,我想textView增加显示数字1,直到73,所以这将是1-2-3-4-5 …等等等我怎么能做到这一点?

Solutions Collecting From Web of "animationTextView增加整数,停止在某个点?"

以下是一个简单的函数,根据初始值和最终值为textView的文本设置animation

public void animateTextView(int initialValue, int finalValue, final TextView textview) { DecelerateInterpolator decelerateInterpolator = new DecelerateInterpolator(0.8f); int start = Math.min(initialValue, finalValue); int end = Math.max(initialValue, finalValue); int difference = Math.abs(finalValue - initialValue); Handler handler = new Handler(); for (int count = start; count <= end; count++) { int time = Math.round(decelerateInterpolator.getInterpolation((((float) count) / difference)) * 100) * count; final int finalCount = ((initialValue > finalValue) ? initialValue - count : count); handler.postDelayed(new Runnable() { @Override public void run() { textview.setText(finalCount + ""); } }, time); } } 

我认为最好的解决scheme是使用这种方法:

 public void animateTextView(int initialValue, int finalValue, final TextView textview) { ValueAnimator valueAnimator = ValueAnimator.ofInt(initialValue, finalValue); valueAnimator.setDuration(1500); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { textview.setText(valueAnimator.getAnimatedValue().toString()); } }); valueAnimator.start(); } 

我觉得这个项目在github是你想要的: https : //github.com/sd6352051/RiseNumber

RiseNumberTextView扩展TextView并使用ValueAnimator来实现上升数字效果。

试试这个代码..显示增量值与animation

 public class MainActivity extends Activity implements AnimationListener { private TextView textView; AlphaAnimation fadeIn, fadeOut; private static int count = 0, finalValue = 20; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); textView = (TextView) findViewById(R.id.textView); fadeIn = new AlphaAnimation(0.0f, 1.0f); fadeOut = new AlphaAnimation(1.0f, 0.0f); fadeIn.setDuration(1000); fadeIn.setFillAfter(true); fadeOut.setDuration(1000); fadeOut.setFillAfter(true); fadeIn.setAnimationListener(this); fadeOut.setAnimationListener(this); textView.startAnimation(fadeIn); textView.startAnimation(fadeOut); } @Override public void onAnimationEnd(Animation arg0) { // TODO Auto-generated method stub Log.i("mini", "Count:" + count); runOnUiThread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub textView.setText("" + count); } }); if (count == finalValue) { textView.setText("" + finalValue); } else { ++count; textView.startAnimation(fadeIn); textView.startAnimation(fadeOut); } } @Override public void onAnimationRepeat(Animation arg0) { // TODO Auto-generated method stub } @Override public void onAnimationStart(Animation arg0) { // TODO Auto-generated method stub } }