2016-02-05 88 views
0

我有一個這樣定義的ImageViewAndroid的圖標動畫改變顏色

<ImageView 
    android:id="@+id/iv_check_simple" 
    android:background="@drawable/ic_check_simple" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" /> 

其中ic_check_simple可繪製的是用彩色#757575材料圖標之一。我假裝圖標的顏色,在用戶按ViewGroup(其中ImageView)後,更改爲#00ff00

我想用下面的代碼來實現這一目標:

int colorFrom = R.color.grey_600; 
int colorTo = R.color.electric_green; 
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); 
colorAnimation.setDuration(250); // milliseconds 
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 

    @Override 
    public void onAnimationUpdate(ValueAnimator animator) { 
    mActivitySelectorIcon.setColorFilter((int) animator.getAnimatedValue(), PorterDuff.Mode.DST_IN); 
    } 
}); 
colorAnimation.start(); 

我相信我已經嘗試了所有的PorterDuff模式,但沒有成功。

我想這在運行棒棒糖設備的工作和預棒棒堂(minSDK 16)

回答

1

的問題是,你是動畫的資源ID,而不是實際的顏色。所以你必須這樣做:

int colorFrom = GetResources().GetColor(R.color.grey_600); 
int colorTo = GetResources().GetColor(R.color.electric_green); 
... 
+1

謝謝。解決了。只需要做到這一點,並改變到'PorterDuff.Mode.SRC_ATOP'來實現我的目標。 – Favolas