2015-10-19 59 views
1

一般情況下,我試圖動畫int類型的數組,不像典型的情況下,你只是一個動畫int或從一個號碼浮到另一個。動畫兩個值同步在Android

具體來說,我試圖在GradientDrawable對象上設置顏色的int []。

GradientDrawable具有指定屬性「setColors(INT [])」,其中我設置2種顏色,起始色和終了色,從而彌補了一個整體梯度。

我想從一種顏色組合向另一種顏色組合。如果是純色的,我已經能夠做到這一點,如下所示:

Integer colorFrom = getResources().getColor(R.color.red); 
Integer colorTo = getResources().getColor(R.color.blue); 
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); 
colorAnimation.addUpdateListener(new AnimatorUpdateListener() { 

    @Override 
    public void onAnimationUpdate(ValueAnimator animator) { 
     textView.setBackgroundColor((Integer)animator.getAnimatedValue()); 
    } 

}); 

colorAnimation.start(); 

所以,我需要這樣的:

//Define 2 starting colors 
Integer colorFrom1 = getResources().getColor(R.color.red); 
Integer colorFrom2 = getResources().getColor(R.color.darkred); 
int[] startColors = new int[] {colorFrom1, colorFrom2}; 

//Define 2 ending colors 
Integer colorTo1 = getResources().getColor(R.color.blue); 
Integer colorTo2 = getResources().getColor(R.color.darkblue); 
int[] endColors = new int[] {colorTo1, colorTo2}; 

ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), startColors, endColors); 
colorAnimation.addUpdateListener(new AnimatorUpdateListener() { 

    @Override 
    public void onAnimationUpdate(ValueAnimator animator) { 
     gradientView.setColors((Integer)animator.getAnimatedValues()[0] , (Integer)animator.getAnimatedValues()[1]); 
    } 

}); 

colorAnimation.start(); 

Obvioulsy將不存在的代碼,因爲沒有getAnimatedValues()方法返回一個數組,並且沒有ValueAnimator.ofObject方法接受一個數組作爲開始和結束值。

任何想法?

我現在唯一的想法是並行運行兩個動畫師,每個動畫漸變的一個維度,每個設置只有一半的數組接受gradientDrawable.setColors()....但boyyyy幾乎無法接受效率低下並且可能危險地失去同步。

回答

2

這個怎麼樣?

public class ArgbArrayEvaluator implements TypeEvaluator<Integer[]> { 
    ArgbEvaluator evaluator = new ArgbEvaluator(); 

    public Integer[] evaluate(float fraction, Integer[] startValues, Integer[] endValues) { 

     if(startValues.length != endValues.length) throw new ArrayIndexOutOfBoundsException(); 
     Integer[] values = new Integer[startValues.length]; 
     for(int = 0;i<startValues.length;i++) { 
      values[i] = (Integer) evaluator.evaluate(fraction,startValues[i],endValues[i]); 
     } 
     return values; 
    } 
} 

然後做

/* Make sure startColors and endColors are Integer[] not int[] */ 
final ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbArrayEvaluator(),startColors,endColors); 

監聽器代碼:

public void onAnimationUpdate(ValueAnimator animator) { 
    Integer[] values = (Integer[]) animator.getAnimatedValue(); 
    gradientView.setColors(values[0],values[1]); 
} 

或者,ObjectAnimator

final ObjectAnimator colorAnimation = ObjectAnimator.ofMultiInt(gradientView,"colors",null, new ArgbArrayEvaluator(), startColors,endColors); 

此外,ValueAnimator文檔中的start()方法中它說:

通過調用此方法開始的動畫將在調用此方法的線程上運行。這個線程應該有一個Looper(如果不是這種情況,將會拋出一個運行時異常)。此外,如果動畫將在視圖層次動畫對象的屬性,然後調用線程應該對此視圖層次的UI線程。

所以我會使用以下,如果你不在UI線程。

runOnUiThread(new Runnable() { 
    public void run() { 
     colorAnimation.start(); 
    } 
}); 

我認爲它會工作!