Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式更改现有形状中的纯色_Java_Android_Android Imageview_Android Drawable - Fatal编程技术网

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

Java 以编程方式更改现有形状中的纯色,java,android,android-imageview,android-drawable,Java,Android,Android Imageview,Android Drawable,我有一个自定义边框的圆形视图,使用库实现,并在RecyclerView中膨胀。到目前为止,我已经在里面放了一个图像,但是现在我想用一个单色的圆形和一个文本来代替它 我创建了以下形状(shape\u colored\u circle.xml): 我知道布局似乎有点奇怪(“为什么需要顶部LinearLayout呢?”),但没有它,视图就不会出现在RecyclerView中,而这个布局(取自库的示例)满足了我们的需要 在onBindViewHolder中,我将形状设置为CircularImageVie

我有一个自定义边框的圆形视图,使用库实现,并在
RecyclerView
中膨胀。到目前为止,我已经在里面放了一个图像,但是现在我想用一个单色的圆形和一个文本来代替它

我创建了以下形状(
shape\u colored\u circle.xml
):

我知道布局似乎有点奇怪(“为什么需要顶部
LinearLayout
呢?”),但没有它,视图就不会出现在
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]);
}
GradientDrawable gd=(GradientDrawable)holder.缩略图.getBackground() 如果(gd!=null){ gd.mutate(); gd.setColor(颜色[位置]); }
`
GradientDrawable gd=(GradientDrawable)holder.缩略图.getBackground()
如果(gd!=null){
gd.mutate();
gd.setColor(颜色[位置]);
}
多亏了我才设法解决了这个案子

首先,我将预定义形状设置为
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"/>
多亏了你,我才设法解决了这个案子

首先,我将预定义形状设置为
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"/>

几年前,我遇到了一个问题,当我试图通过编程设置颜色时,我的绘图表的背景变成了黑色。也许是类似的问题@0x5453 I添加了
gd.setAlpha()
。对于值255,它是一个黑色矩形;对于值0,它(显然)不可见。您认为您的解决方案与通过编程创建的可绘制文件相关吗?几年前,当我尝试通过编程设置颜色时,遇到了一个问题,即我的可绘制文件的背景变为黑色。也许是类似的问题@0x5453 I添加了
gd.setAlpha()
。对于值255,它是一个黑色矩形;对于值0,它(显然)不可见。您认为您的解决方案与通过编程创建的绘图相关吗?问题是
CircularImageView
background是圆圈后面的整个矩形。因此,我必须设置源,而不是背景。问题是
CircularImageView
background是圆后面的整个矩形。因此,我必须设置源,而不是背景。这是我方法的精确副本,不是吗?不,这里
shape\u colored\u circle
用作源,而不是背景。这是我方法的精确副本,不是吗?不,这里
shape\u colored\u circle
用作源,而不是背景。
<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"/>
GradientDrawable gd = (GradientDrawable) holder.thumbnail.getDrawable();
if (gd != null) {
    gd.setColor(colors[position]);
}