2013-07-24 138 views
99

我在编辑以使问题更简单,希望能够帮助您获得准确的答案。以编程方式设置android形状颜色

说我有以下oval形状:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> 
    <solid android:angle="270" 
      android:color="#FFFF0000"/> 
    <stroke android:width="3dp" 
      android:color="#FFAA0055"/> 
</shape> 

如何设置的颜色编程,从活动类中?

+0

你对此drawable设置了什么? – Vikram

+0

drawable是一个“椭圆形”,是ImageView的背景。 –

+0

如果问这个问题太困难了,是否有办法将多个图像绘制到画布上,并将分层最终产品设置为视图的背景? –

回答

12

试试这个:

public void setGradientColors(int bottomColor, int topColor) { 
GradientDrawable gradient = new GradientDrawable(Orientation.BOTTOM_TOP, new int[] 
{bottomColor, topColor}); 
gradient.setShape(GradientDrawable.RECTANGLE); 
gradient.setCornerRadius(10.f); 
this.setBackgroundDrawable(gradient); 
} 

的详细检查此链接this

希望帮助。

+0

投票支持链接。但这不是我的问题的答案。 –

177

:答案已经更新到覆盖场景backgroundColorDrawable一个实例。谢谢Tyler Pfaff,指出这一点。

的绘制是一个椭圆形,是一个ImageView的

的背景使用getBackground()获取从imageViewDrawable

if (background instanceof ShapeDrawable) { 
    // cast to 'ShapeDrawable' 
    ShapeDrawable shapeDrawable = (ShapeDrawable) background; 
    shapeDrawable.getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} else if (background instanceof GradientDrawable) { 
    // cast to 'GradientDrawable' 
    GradientDrawable gradientDrawable = (GradientDrawable) background; 
    gradientDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} else if (background instanceof ColorDrawable) { 
    // alpha value may need to be set again after this call 
    ColorDrawable colorDrawable = (ColorDrawable) background; 
    colorDrawable.setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} 

Drawable background = imageView.getBackground(); 

对通常的嫌疑人入住

压缩版本:

Drawable background = imageView.getBackground(); 
if (background instanceof ShapeDrawable) { 
    ((ShapeDrawable)background).getPaint().setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} else if (background instanceof GradientDrawable) { 
    ((GradientDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} else if (background instanceof ColorDrawable) { 
    ((ColorDrawable)background).setColor(ContextCompat.getColor(mContext,R.color.colorToSet)); 
} 

请注意,不需要空值检查。

但是,在修改它们之前,如果它们在其他地方使用,则应在画面上使用mutate()。 (默认情况下,图形的XML共享加载相同的状态。)

+3

感谢您的回答。 (+1)。我的代码遇到其他错误,所以很难测试。但是,这仍然可以设定形状的“坚实”部分。 'stroke'部分怎么样? –

+0

干啥管理?我期待着做同样的事情! – TiGer

+1

@TiGer您应该在您的评论中添加“@ username”以确保向用户发送通知。顺便说一下,您需要继承'ShapeDrawable'以设置笔划部分。更多信息在这里:[链接](http://stackoverflow.com/a/3663956)。看看评论,因为它提到了接受答案的问题。 – Vikram

38

这样做:

ImageView imgIcon = findViewById(R.id.imgIcon); 
    GradientDrawable backgroundGradient = (GradientDrawable)imgIcon.getBackground(); 
    backgroundGradient.setColor(getResources().getColor(R.color.yellow)); 
+0

尝试它,但得到NPE。 – user3111850

+0

@ user3111850在你调用'getBackground()'之前,你在xml中甚至是'setBackground'中添加了'android:background'吗?如果你这样做的话,它应该有效。 –

9

希望这将有助于有人用同样的问题

GradientDrawable gd = (GradientDrawable) YourImageView.getBackground(); 
//To shange the solid color 
gd.setColor(yourColor) 

//To change the stroke color 
int width_px = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, youStrokeWidth, getResources().getDisplayMetrics()); 
gd.setStroke(width_px, yourColor); 
+0

这工作正常,谢谢 – Leebeedev

+0

最初我无法得到这个工作,我想出** yourColor **必须提供像这样:'gd.setStroke(width_px,Color.parseColor(“#FF5722”)); ' – pwnsauce

5

这是解决方案,为我工作...写在另一个问题以及: How to change shape color dynamically?

//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) 
6

Vikram's的答案上展开,如果您正在为动态视图着色,如回收器视图项目等....那么您可能要在设置颜色之前调用mutate()。如果你不这样做,任何具有通用可绘制视图(即背景)的视图也会将其可绘制改为/着色。

public static void setBackgroundColorAndRetainShape(final int color, final Drawable background) { 

    if (background instanceof ShapeDrawable) { 
     ((ShapeDrawable) background.mutate()).getPaint().setColor(color); 
    } else if (background instanceof GradientDrawable) { 
     ((GradientDrawable) background.mutate()).setColor(color); 
    } else if (background instanceof ColorDrawable) { 
     ((ColorDrawable) background.mutate()).setColor(color); 
    }else{ 
     Log.w(TAG,"Not a valid background type"); 
    } 

} 
+1

需要额外的检查和参数:'if(background instanceof LayerDrawable)background =((LayerDrawable)background.mutate())。getDrawable(indexIfLayerDrawable); } if(background instanceof ShapeDrawable)[...]'处理使用' Johny

3

这个问题已经回答了一段时间,但它可以现代化改写为kotlin扩展函数。

​​
相关问题