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

标签 java android android-imageview android-drawable

我有一个带有自定义边框的圆形 View ,使用 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>

并添加了一个TextView连同 CircularImageViewFrameLayout :

<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>

我知道布局看起来有点奇怪(“为什么顶部 LinearLayout 是必需的?”),但如果没有它, View 就不会出现在 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);

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

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

谢谢!

最佳答案

   <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]);
}

关于java - 以编程方式更改现有形状中的纯色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40240137/

相关文章:

android - 如何在android中将图像从一个 Activity 发送到另一个 Activity ?

java - jMonkeyEngine相机跟随

数据绑定(bind)不起作用的 Android gradle-experimental?

java - 调用方法时处理空指针异常

android - 在 CountdownTimer 上获取剩余时间并将剩余时间用作分数 android

android - OnListItemClick 在 Activity 中

android - 如何从中心裁剪为 ImageView 设置动画以填充屏幕,反之亦然(facebook 风格)?

android - 在 ImageView 的 setImageBitmap 之后 onmeasure 方法上的时间太多

java - 具有静态初始化程序 block 的单例

java - 尝试在 java 中连接 2 个表,但出现错误