注:答案已经更新到覆盖场景background
是ColorDrawable
一个实例。谢谢Tyler Pfaff,指出这一点。
的绘制是一个椭圆形,是一个ImageView的
的背景使用getBackground()
获取从imageView
的Drawable
:
if (background instanceof ShapeDrawable) {
// cast to 'ShapeDrawable'
ShapeDrawable shapeDrawable = (ShapeDrawable) background;
shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
// cast to 'GradientDrawable'
GradientDrawable gradientDrawable = (GradientDrawable) background;
gradientDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
// alpha value may need to be set again after this call
ColorDrawable colorDrawable = (ColorDrawable) background;
colorDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}
:
Drawable background = imageView.getBackground();
对通常的嫌疑人入住
压缩版本:
Drawable background = imageView.getBackground();
if (background instanceof ShapeDrawable) {
((ShapeDrawable)background).getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof GradientDrawable) {
((GradientDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
} else if (background instanceof ColorDrawable) {
((ColorDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet));
}
请注意,不需要空值检查。
但是,在修改它们之前,如果它们在其他地方使用,则应在画面上使用mutate()
。 (默认情况下,图形的XML共享加载相同的状态。)
你对此drawable设置了什么? – Vikram
drawable是一个“椭圆形”,是ImageView的背景。 –
如果问这个问题太困难了,是否有办法将多个图像绘制到画布上,并将分层最终产品设置为视图的背景? –