如何动态更改形状颜色?

138

我有

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid
       android:color="#FFFF00" />
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
</shape>

<TextView
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"
/>

现在我希望这个形状根据从Web服务调用返回的信息改变颜色。因此,根据我从Web服务调用接收到的颜色,它可以是黄色、绿色、红色或其他任何颜色。

如何基于这些信息更改形状的颜色?


3
根据@Couitchy方法指定,View.getBackground()返回一个GradientDrawable而不是ShapeDrawable。这导致在运行时尝试获取引用并以编程方式设置颜色时出现无效转换而导致应用程序崩溃。[Android形状文档] (http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)指出:*已编译资源数据类型:指向`GradientDrawable`的资源指针*。 - Luis Quijada
可能是[以编程方式设置Android形状颜色]的重复问题(https://dev59.com/HmMm5IYBdhLWcg3wKsgj)。 - Amin Soheyli
14个回答

304
你可以简单地像这样修改它。
GradientDrawable bgShape = (GradientDrawable)btn.getBackground();
bgShape.setColor(Color.BLACK);

9
btn.getBackground是什么? - chobo2
16
尝试这个建议时,我得到了“java.lang.ClassCastException:android.graphics.drawable.GradientDrawable无法转换为android.graphics.drawable.ShapeDrawable”的错误。 - prolink007
2
不起作用。您将收到一个转换错误。需要修复或接受另一个答案。 - ndgreen
7
这个很好用。由于答案已经被编辑过了,之前的评论已经过时了! - W3hri
4
应该使用GradientDrawable bgShape = (GradientDrawable)btn.getBackground().getCurrent(); 说明:这行代码用于获取一个按钮控件的背景,并将其转换为GradientDrawable对象类型,以便对其进行进一步的操作。 - naveed148
显示剩余11条评论

61

对我来说,它崩溃的原因是getBackground返回了一个GradientDrawable而不是ShapeDrawable。所以我进行了修改:

((GradientDrawable)someView.getBackground()).setColor(someColor);

46

这对我有用,使用一个初始的xml资源:

example.setBackgroundResource(R.drawable.myshape);
GradientDrawable gd = (GradientDrawable) example.getBackground().getCurrent();
gd.setColor(Color.parseColor("#000000"));
gd.setCornerRadii(new float[]{30, 30, 30, 30, 0, 0, 30, 30});
gd.setStroke(2, Color.parseColor("#00FFFF"), 5, 6);

上述操作的结果: http://i.stack.imgur.com/hKUR7.png


这是正确的答案。上面投票最高的两个答案对我都没有用。我在两个答案中都遇到了异常。 - Sanal Varghese

11

您可以在Java中构建自己的形状。我曾为类似iPhone页面控制器的东西构建了这样的形状,并在Java中绘制了它们:

/**
 * Builds the active and inactive shapes / drawables for the page control
 */
private void makeShapes() {

    activeDrawable = new ShapeDrawable();
    inactiveDrawable = new ShapeDrawable();
    activeDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);
    inactiveDrawable.setBounds(0, 0, (int) mIndicatorSize,
            (int) mIndicatorSize);

    int i[] = new int[2];
    i[0] = android.R.attr.textColorSecondary;
    i[1] = android.R.attr.textColorSecondaryInverse;
    TypedArray a = this.getTheme().obtainStyledAttributes(i);

    Shape s1 = new OvalShape();
    s1.resize(mIndicatorSize, mIndicatorSize);
    Shape s2 = new OvalShape();
    s2.resize(mIndicatorSize, mIndicatorSize);

    ((ShapeDrawable) activeDrawable).getPaint().setColor(
            a.getColor(0, Color.DKGRAY));
    ((ShapeDrawable) inactiveDrawable).getPaint().setColor(
            a.getColor(1, Color.LTGRAY));

    ((ShapeDrawable) activeDrawable).setShape(s1);
    ((ShapeDrawable) inactiveDrawable).setShape(s2);
}

希望这可以帮到你。 问候,Fabian


11

也许有其他人需要在XML中更改颜色,但不像我一样需要创建多个可绘制对象。那么可以创建一个没有颜色的圆形可绘制对象,然后为ImageView指定backgroundTint。

circle.xml

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

在您的布局中:

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@drawable/circle"
    android:backgroundTint="@color/red"/>

编辑:

关于这个方法有一个bug,它导致在Android Lollipop 5.0(API级别21)上无法使用,但已在更新的版本中修复。


6
    LayerDrawable bgDrawable = (LayerDrawable) button.getBackground();
    final GradientDrawable shape = (GradientDrawable)
            bgDrawable.findDrawableByLayerId(R.id.round_button_shape);
    shape.setColor(Color.BLACK);

在形状 XML 文件中,round_button_shape 应该定义在哪里? - Jayesh

5

circle.xml (可绘制对象)

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

<solid
    android:color="#000"/>

<size
    android:width="10dp"
    android:height="10dp"/>
</shape>

布局
<ImageView
      android:id="@+id/circleColor"
      android:layout_width="15dp"
       android:layout_height="15dp"
      android:textSize="12dp"
       android:layout_gravity="center"
       android:layout_marginLeft="10dp"
      android:background="@drawable/circle"/>

在活动中
   circleColor = (ImageView) view.findViewById(R.id.circleColor);
   int color = Color.parseColor("#00FFFF");
   ((GradientDrawable)circleColor.getBackground()).setColor(color);

不是完美的,但它帮助我找到了解决方案。 - Rakesh Yadav

2

这个解决方案对我来说很有效,使用的是Android SDK v19:

//get the image button by id
ImageButton myImg = (ImageButton) findViewById(R.id.some_id);

//get drawable from image button
GradientDrawable drawable = (GradientDrawable) myImg.getDrawable();

//set color as integer
//can use Color.parseColor(color) if color is a string
drawable.setColor(color)

2
如果您有这样一个imageView:
   <ImageView
    android:id="@+id/color_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dp"
    android:src="@drawable/circle_color"/>

如果您想将形状的颜色改变,可以使用以下代码,其中形状作为src:

ImageView iv = (ImageView)findViewById(R.id.color_button);
GradientDrawable bgShape = (GradientDrawable)iv.getDrawable();
bgShape.setColor(Color.BLACK);

2
最简单的使用 Radius 填充形状的方法是:

XML:

<TextView
    android:id="@+id/textView"
    android:background="@drawable/test"
    android:layout_height="45dp"
    android:layout_width="100dp"
    android:text="Moderate"/>

Java:

(textView.getBackground()).setColorFilter(Color.parseColor("#FFDE03"), PorterDuff.Mode.SRC_IN);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接