【发布时间】:2018-03-27 21:08:19
【问题描述】:
在 android 上,我正在尝试创建一个倒计时动画,它可以做两件事:
- 它将正方形从绿色渐变为白色
- 在执行时会更改文本视图的上下文
代码是:
int colorFrom = getResources().getColor(R.color.green);
int colorTo = getResources().getColor(R.color.white);
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
colorAnimation.setRepeatMode(ValueAnimator.RESTART);
colorAnimation.setDuration(30000); // milliseconds
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animator)
{
tv.setBackgroundColor((int) animator.getAnimatedValue());
}
});
colorAnimation.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animation)
{
String authcode = getAuthCode(login_session);
code.setText(authcode);
}
});
colorAnimation.start();
我知道Detecting when ValueAnimator is done,这是我将解决方案移植到我的代码中的地方,但它似乎不适用于我的情况。绿色到白色的淡入淡出工作,但完成后它只是重新启动动画而不在文本视图中设置新文本。
我做错了什么以及如何在动画结束时设置新文本?
【问题讨论】:
-
你试过设置colorAnimation.setRepeatCount(0);并删除 colorAnimation.setRepeatMode(ValueAnimator.RESTART);这一行?
-
试试这个 colorAnimation.setRepeatCount(0);