我有一个椭圆形的可绘制对象,其中带有对勾标记。 是否可以通过编程方式更改椭圆形而不更改选中标记的颜色?
这是我的可绘制对象:
<item>
<shape
android:shape="oval">
<solid android:color="@color/black" />
</shape>
</item>
<item>
<bitmap
android:src="@drawable/check_mark"/>
</item>
我只想通过编程将纯黑色更改为其他颜色
答案 0 :(得分:0)
只用其他“椭圆”颜色添加第二个可绘制对象,然后以编程方式替换可绘制对象会更容易。
答案 1 :(得分:0)
您可以使用以下参考代码语法地创建形状。
GradientDrawable shape = new GradientDrawable();
shape.setCornerRadius(24);
shape.setShape(GradientDrawable.OVAL);
shape.setColor(R.color.red);
imageView.setBackground(shape);
答案 2 :(得分:0)
drawable是一个椭圆形,是ImageView的背景
使用getBackground()从imageView获取Drawable:
Drawable background = imageView.getBackground();
与普通嫌疑犯进行核对:
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();
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));
}