以编程方式更改现有形状中的纯色

Programmatically change solid color in an existing shape

我有一个带有自定义边框的圆形视图,使用 CircularImageView 库实现并在 RecyclerView 中膨胀。到目前为止,我已经在其中放了一张图片,但现在我想用单色圆圈和上面的文字替换它。

我创建了以下形状 (shape_colored_circle.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size
        android:height="70dp"
        android:width="70dp"/>
    <solid
        android:color="#00f"/>
</shape>

并在 FrameLayout 中添加 TextViewCircularImageView:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/icon"
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_thumbnail"/>

        <TextView
            android:id="@+id/card_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/app_name"
            android:textSize="18sp"
            android:textStyle="bold"/>

    </FrameLayout>

</LinearLayout>

我知道布局看起来有点奇怪 ("why is the top LinearLayout necessary?"),但如果没有它,视图根本不会出现在 RecyclerView 而这个布局(取自图书馆的示例)满足我们的需求。

onBindViewHolder 中,我将形状设置为 CircularImageView 资源:

holder.thumbnail.setImageDrawable(activity.getResources().getDrawable(R.drawable.shape_colored_circle));

终于我看到了我所期待的。问题是我的可绘制对象预先设置了特定的颜色。因此,我尝试以编程方式创建形状:

GradientDrawable gd = new GradientDrawable();
gd.setShape(GradientDrawable.RECTANGLE);
gd.setColor(colors[position]);             //Getting the color from a predefined array of colors
holder.thumbnail.setImageDrawable(gd);

但是视图只是变成了一个黑色的矩形。我尝试了更多以编程方式创建形状的解决方案(所有这些都非常相似)但无济于事 - 我要么看到一个黑色矩形,要么什么都看不到。

我是不是做错了什么?有没有另一种方法来创建形状,或者有什么方法可以在运行时改变现有的形状?或者也许是一种完全不同的方式来实现所需的行为?

谢谢!

   <com.mikhaellopez.circularimageview.CircularImageView
    android:id="@+id/card_thumbnail"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:background="@drawable/shape_colored_circle" //Note
    android:contentDescription="@string/card_thumbnail"
    android:src="@drawable/icon"
    app:civ_border="true"
    app:civ_border_color="@color/border_color"
    app:civ_border_width="2dp"
    app:civ_shadow="true"
    app:civ_shadow_color="@color/grey"
    app:civ_shadow_radius="10"/>`

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getBackground()
if (gd != null) {
  gd.mutate();
  gd.setColor(colors[position]);
}

多亏了Vijay Pal Vishwakarma才破案

首先,我将预定义形状设置为 CircularImageView:

<com.mikhaellopez.circularimageview.CircularImageView
            android:id="@+id/card_thumbnail"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/shape_colored_circle"      //Note this line
            app:civ_border="true"
            app:civ_border_color="@color/border_color"
            app:civ_border_width="2dp"
            app:civ_shadow="true"
            app:civ_shadow_color="@color/grey"
            app:civ_shadow_radius="10"
            android:contentDescription="@string/card_followee_large_thumbnail"/>

然后,在 onBindViewHolder 中,我获取现有的可绘制对象并更改其颜色:

GradientDrawable gd = (GradientDrawable) holder.thumbnail.getDrawable();
if (gd != null) {
    gd.setColor(colors[position]);
}