我需要在android中創建一個圓形並設置爲圖像背景,有兩個共享相同形狀的多個圖像,因此我需要爲每個圖像設置不同的背景。在Android中動態更改圖形顏色
這裏是形狀代碼:
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/colorWhite"/>
</shape>
</item>
在XML文件:
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="2dp"
android:src="@drawable/color"
android:tag="#FF660000" />
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_margin="2dp"
android:src="@drawable/color"
android:tag="#FFFF0000" />
默認情況下,形狀有白色,但我需要爲不同的圖像,我怎麼能做到這一點設置不同。
它實際上是一個油漆應用程序,用戶可以從特定的給定顏色中選擇顏色。
請讓我知道如何更改形狀顏色?
更新
的答案是提供它不工作了,也是我檢查的意見有以下的答案是不正確的。我檢查了我的自我,它給錯誤。
檢查了這一點:
btnCapturePicture = (ImageView) view.findViewById(R.id.btnCapturePicture);
if(btnCapturePicture != null) {
GradientDrawable bgShape = (GradientDrawable) btnCapturePicture.getBackground();
bgShape.setColor(Color.BLUE);
}
錯誤:Attempt to invoke virtual method 'void android.graphics.drawable.GradientDrawable.setColor(int)' on a null object reference
我怎樣才能解決呢?
@Redman任何的例子嗎? –
可能重複[如何動態改變形狀顏色?](https://stackoverflow.com/questions/7164630/how-to-change-shape-color-dynamically) – Redman
@Redman檢查我的問題我編輯它,鏈接你提供的不再工作 –