使用软动画更改视图背景颜色无限Android

时间:2017-04-03 10:49:18

标签: android animation

我想创建一个在我的视图中连续运行的变色背景动画,并且我已经能够使用此代码创建变色动画。

int prevColor = getRandomColor();
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), prevColor, getRandomColor(prevColor));
colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
colorAnimation.setDuration(transitionTime); // milliseconds
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        backgroundBase.setBackgroundColor((int) animator.getAnimatedValue());
    }
});
colorAnimation.start();

运行顺利,但......

  1. 当动画结束时,它突然变回第一次没有软动画的第一次变色

  2. 然后,我想做的是当动画结束时,我希望动画从我的颜色列表中获取新的随机颜色,以便颜色不时变化(不仅仅是来回反转) )

  3. 我在setAnimationListener中找不到colorAnimation,因此我无法使用覆盖方法onAnimationEnd

  4. 我该怎么做才能实现这一目标?

3 个答案:

答案 0 :(得分:1)

由于Adrian Coman

,我终于成功了

我不使用ValueAnimator.ofObject(new ArgbEvaluator(), prevColor, getRandomColor(prevColor));来初始化colorAnimation,而是使用默认构造函数。并使用setIntValues更改onAnimationRepeat

中的背景值
int defaultBackground = getRandomColor();
final ValueAnimator colorAnimation = new ValueAnimator();
colorAnimation.setIntValues(defaultBackground, getRandomColor(defaultBackground));
colorAnimation.setEvaluator(new ArgbEvaluator());
colorAnimation.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {

    }
    @Override
    public void onAnimationEnd(Animator animation) {

    }
    @Override
    public void onAnimationCancel(Animator animation) {

    }
    @Override
    public void onAnimationRepeat(Animator animation) {
       int backgroundColor = ((ColorDrawable) backgroundBase.getBackground()).getColor();
       int nextColor = getRandomColor(backgroundColor);
       colorAnimation.setIntValues(backgroundColor, nextColor);
       colorAnimation.start();
    }
});
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animator) {
        backgroundBase.setBackgroundColor((int) animator.getAnimatedValue());
    }
});
colorAnimation.setDuration(transitionTime); // milliseconds
colorAnimation.setRepeatCount(ValueAnimator.INFINITE);
colorAnimation.start();

它每隔2秒(我的过渡时间)无限地改变背景颜色值。但我仍然不知道为什么我需要colorAnimation.start();

中的onAnimationRepeat

我试图删除它,但背景颜色随闪烁动画而变化,而不是像第一次动画时那样柔和变换颜色动画。

答案 1 :(得分:0)

  1. 将fillAfter设置为true:

    colorAnimation.setFillAfter(true);
    
  2. 将nextColor保存到prevColor变量,以便您可以从那里开始动画。从onAnimationEnd调用包含ValueAnimator的方法。不要认为你需要setRepeatCount()。
  3. 您可以使用addListener:

    colorAnimation.addListener(new Animation.AnimatorListener(){....})
    

答案 2 :(得分:0)

这是因为您正在更改初始动画的颜色...如果您使用视图的当前颜色开始动画,它可能会起作用。

试试这个:

    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), backgroundBase.getBackgroundColor, 
getRandomColor(backgroundBase.getBackgroundColor));
    colorAnimation.setRepeatCount(ValueAnimator.INFINITE); 
    colorAnimation.setDuration(transitionTime); // milliseconds 
    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
        @Override 
        public void onAnimationUpdate(ValueAnimator animator) {
            backgroundBase.setBackgroundColor((int) animator.getAnimatedValue());
        } 
    }); 
    colorAnimation.start();
相关问题