我有一个TextView,我想倒数(3 ... 2 ... 1 ...发生的东西)。Android简单的TextView动画
为了让它更有趣一点,我希望每个数字都以完全不透明的方式开始,然后淡入透明。
有没有简单的方法来做到这一点?
我有一个TextView,我想倒数(3 ... 2 ... 1 ...发生的东西)。Android简单的TextView动画
为了让它更有趣一点,我希望每个数字都以完全不透明的方式开始,然后淡入透明。
有没有简单的方法来做到这一点?
尝试是这样的:
private void countDown(final TextView tv, final count) {
if (count == 0) {
tv.setText(""); //Note: the TextView will be visible again here.
return;
}
tv.setText(count);
AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(1000);
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
countDown(tv, count - 1);
}
... //implement the other two methods
});
tv.startAnimation(animation);
}
我只是打字出来,所以它可能无法编译原样。
我首先尝试了@dmon解决方案,但是由于每个动画都是在前一个动画的结尾处开始的,因此在多次调用之后最终会出现延迟。
因此,我实现了CountDownAnimation
类,它使用了Handler
和postDelayed
函数。默认情况下,它使用alpha动画,但可以设置任何动画。您可以下载项目here。
我用更传统的Android风格的动画,这一点:
ValueAnimator animator = new ValueAnimator();
animator.setObjectValues(0, count);
animator.addUpdateListener(new AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
view.setText(String.valueOf(animation.getAnimatedValue()));
}
});
animator.setEvaluator(new TypeEvaluator<Integer>() {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
return Math.round((endValue - startValue) * fraction);
}
});
animator.setDuration(1000);
animator.start();
您可以用0
和count
值起到使计数器任意数量的去到任何数量,以及与玩1000
设置整个动画的持续时间。
请注意,这支持Android API级别11及以上,但您可以使用真棒nineoldandroids项目使其轻松向后兼容。
用'tv.setText(String.valueOf(count))'替换'tv.setText(count);''和代码工作正常 – 2015-03-24 23:00:00