如何在 Android 中设置图像的背景颜色

how to set background color of image in Android

我有 png 图像,图像之间有一些透明区域。我正在尝试以编程方式为该区域设置颜色,但它不起作用。

这是我的xml布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<com.mikhaellopez.circularimageview.CircularImageView
    android:id="@+id/iv_cat_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/del"
    card_view:civ_border_color="@color/tabsScrollColor" />
   </RelativeLayout>

在 xml 我有自定义属性

  xmlns:card_view="http://schemas.android.com/apk/res-auto

在 xml 中使用此 card_view 我可以设置颜色,但我需要以编程方式更改其 color.is 无论如何我可以以编程方式设置此 属性 吗?请任何人帮助我。

CircularImageView 是自定义视图。您可以更改它的属性,如下所示。

 CircularImageView circularImageView = (CircularImageView)findViewById(R.id.iv_cat_icon);
    // Set Border
    circularImageView.setBorderColor(getResources().getColor(R.color.tabsScrollColor));
    circularImageView.setBorderWidth(10);
CircularImageView imageView = (CircularImageView)findViewById(R.id.iv_cat_icon);

Drawable color = new ColorDrawable(getResources().getColor(R.color.your_color));
Drawable image = getResources().getDrawable(R.drawable.your_drawable);

LayerDrawable ld = new LayerDrawable(new Drawable[]{color, image});
imageView.setImageDrawable(ld);