2017-04-03 51 views
0

我想創建一個不斷變化的背景動畫,並在我的視圖中持續運行,並且我已經能夠使用此代碼創建顏色更改動畫。使用軟動畫更改視圖背景色無限Android

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. 我找不到在colorAnimationsetAnimationListener,所以我不能使用覆蓋方法onAnimationEnd

我應該怎麼做才能做到這一點?

回答

0

我終於能做到這得益於Adrian Coman

我不使用ValueAnimator.ofObject(new ArgbEvaluator(), prevColor, getRandomColor(prevColor));到initalize我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

我試圖將其刪除,但背景顏色變化與閃爍的動畫,而不是軟變色動畫像第一次動畫。

0
  1. 集fillAfter爲true:

    colorAnimation.setFillAfter(true); 
    
  2. 保存nextColor到prevColor變量,這樣就可以從那裏開始的動畫。從onAnimationEnd調用包含ValueAnimator的方法。不要以爲你需要setRepeatCount()。
  3. 您可以使用addListener:

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

ValueAnimator中沒有setFillAfter方法。 我終於設法使用'addListener(new Animator.AnimatorListener(){... onAnimationRepeat(Animator animation(){...})})' – fadeltd

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(); 
+0

它不會改變任何內容,因爲什麼我需要所以它不會馬上回到最初的背景是'colorAnimation.setRepeatMode(ValueAnimator.REVERSE);' – fadeltd