2016-09-28 66 views
-1

我正在嘗試使用對象動畫製作動畫顏色,但它不會改變顏色。她是代碼示例對象動畫不起作用

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

    colorAnimator=ObjectAnimator.ofArgb(button.getBackground().mutate(),"tint", 
      getResources().getColor(R.color.colorAccent),getResources().getColor(R.color.colorPrimaryDark)); 
     colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 
     colorAnimator.start(); 

}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){ 

     colorAnimator=ObjectAnimator.ofObject(button.getBackground().mutate(),"tint",new ArgbEvaluator(), 
      getResources().getColor(R.color.colorAccent),getResources().getColor(R.color.colorPrimaryDark)); 
    colorAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); 
     colorAnimator.start(); 
} 

我還沒有添加對象動畫器對象到按鈕?

回答

1

嘗試此方法用於動畫視圖的背景色 -

public static void animateImageViewBackground(final View viewToAnimate, int colorFrom, int colorTo, int animationDuration) 
{ 

    ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); 

    colorAnimation.setDuration(animationDuration); 

    colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
     @Override 
     public void onAnimationUpdate(ValueAnimator animator) { 
      int newColor = (int) animator.getAnimatedValue(); 
      viewToAnimate.setBackgroundColor(newColor); 
     } 
    }); 
    colorAnimation.start(); 
}