以编程方式设置android形状颜色

我正在编辑,使问题更简单,希望有助于准确的答案。

说我有以下oval形状:

      

如何从活动类中以编程方式设置颜色?

注意 :答案已更新,以涵盖backgroundColorDrawable实例的ColorDrawable 。 感谢泰勒普法夫 ,指出这一点。

drawable是一个椭圆形,是ImageView的背景

使用getBackground()imageView获取Drawable

 Drawable background = imageView.getBackground(); 

检查通常的嫌疑人:

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

请注意,空检查不是必需的。

但是,如果在别处使用drawable,则应在修改前使用mutate() 。 (默认情况下,从XML加载的drawable共享相同的状态。)

这样做:

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

尝试这个:

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

有关更多详细信息,请查看此链接

希望有所帮助

希望这会帮助有同样问题的人

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

在Vikram的答案上展开,如果你正在为动态视图着色,比如回收器视图项目等….那么你可能想要在设置颜色之前调用mutate()。 如果你不这样做,任何有一个共同的drawable(即背景)的意见也将他们drawable改变/着色。

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

这是为我工作的解决方案…写在另一个问题,以及: 如何动态地改变形状的颜色?

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

这个问题已经回答了一段时间,但是可以通过改写为kotlin扩展function来实现现代化。

 fun Drawable.overrideColor(@ColorInt colorInt: Int) { when (this) { is GradientDrawable -> setColor(colorInt) is ShapeDrawable -> paint.color = colorInt is ColorDrawable -> color = colorInt } }