Android 设置TextView动画以增加整数并在某个点停止?
我有一个显示整数值的文本视图。整数值是从上一个活动传输的,我想添加漂亮的动画。例如,如果int值是73,我希望textView将显示的数字增加1直到73,所以它将是1-2-3-4-5…等等。Android 设置TextView动画以增加整数并在某个点停止?,android,Android,我有一个显示整数值的文本视图。整数值是从上一个活动传输的,我想添加漂亮的动画。例如,如果int值是73,我希望textView将显示的数字增加1直到73,所以它将是1-2-3-4-5…等等。 我如何才能做到这一点?试试下面的代码..用动画显示增量值 public class MainActivity extends Activity implements AnimationListener { private TextView textView; AlphaAnimation f
我如何才能做到这一点?试试下面的代码..用动画显示增量值
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
}
}
下面是一个根据初始值和最终值设置textView文本动画的简单函数
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(String.valueOf(finalCount));
}
}, time);
}
}
public void animateTextView(int initialValue、int finalValue、final TextView TextView){
减速插补器减速插补器=新减速插补器(0.8f);
int start=Math.min(初始值,最终值);
int end=Math.max(初始值,最终值);
int差=Math.abs(finalValue-initialValue);
Handler=newhandler();
对于(int count=start;count finalValue)?initialValue-count:count;
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
textview.setText(String.valueOf(finalCount));
}
},时间);
}
}
我认为github中的这个项目正是您想要的:
RiseNumberTextView扩展了TextView,并使用ValueAnimator来实现上升数字效果。我认为最好的解决方案是使用以下方法:
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();
}
这是一个Kotlin代码,用于在一段时间内从初始值递增到最终值。这里我使用了5秒的持续时间
fun animateTextView(initialValue: Int, finalValue: Int, textView: TextView) {
val valueAnimator = ValueAnimator.ofInt(initialValue, finalValue)
valueAnimator.duration = 5000 // 5 sec
valueAnimator.addUpdateListener { valueAnimator -> textView.text = valueAnimator.animatedValue.toString() }
valueAnimator.start()
}
在实用程序中使用此代码,并使用所需的参数相应地调用该方法。您知道这一点吗:您想要动画为什么??我的意思是,你可以简单地使用for循环并在textview中设置值..然后使用thread或timer在特定的时间间隔上更新文本视图我不知道如何使用NumberPicker实现我想要的。我的文本视图更像是一个乐谱。@MeenalSharma嗯,它看起来像是动画,但不一定非得如此。您的想法看起来是一个解决方案。请尝试此最佳功能!工作完美!精彩的!不过,请注意:无需将
initialValue
和finalValue
强制转换为int,因为ValueAnimator.ofInt
需要整数;-)哎呀!工作起来很有魅力。Pedro Oliveira的上述解决方案不适用于从最大到最小的数字动画。。。。这个解决方案太棒了。。。动画流畅…性能更好。这是很棒的+1完美答案。谢谢。这家伙应该得到一个厨师。使用动画师而不是直接处理它不是更好吗?