【问题标题】:How can I change shape color programmatically? [duplicate]如何以编程方式更改形状颜色? [复制]
【发布时间】:2015-12-08 10:20:45
【问题描述】:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="@dimen/radius_default"/>
    <solid
        android:color="@color/alpha"/>

    <stroke
        android:width="1dp"
        android:color="@color/red"/>

</shape>

我想在运行时改变描边的颜色。

我使用了这段代码,但它不起作用。

Drawable drawable = getResources().getDrawable(R.drawable.border_red);
        drawable.setColorFilter(Color.BLUE), PorterDuff.Mode.SRC_ATOP);
        container.setBackground(drawable);

如何改变笔画的颜色?


        float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
        RectF inset = new RectF(6, 6, 6, 6);
        float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
        ShapeDrawable biggerCircle = new ShapeDrawable(new RoundRectShape(outerR,inset, innerR));
        biggerCircle.setIntrinsicHeight( 60 );
        biggerCircle.setIntrinsicWidth( 60);
        biggerCircle.setBounds(new Rect(30, 30, 30, 30));
        biggerCircle.getPaint().setColor(Color.BLUE); 
        border.setBackgroundDrawable(biggerCircle);

我还没有重构,但这段代码对我有用。谢谢你

【问题讨论】:

  • @Lakhan 谢谢! ^^
  • @jihoonkim 很高兴它成功了

标签: android


【解决方案1】:

运行时形状看起来不如 xml,但我们可以创建形状,我们可以更改它的背景颜色以及边框颜色。

我创建了一种可能对你有帮助的方法

   public static LayerDrawable getDrawable(Shape shape, int colorBGNormal, int colorBorderNormal, int defaultStrokeWidth) {

    Drawable[] layers = new Drawable[2];
    ShapeDrawable background = new ShapeDrawable();
    ShapeDrawable backgroundBorder = new ShapeDrawable();

    background.setShape(shape);
    background.getPaint().setColor(colorBGNormal);
    background.getPaint().setAntiAlias(true);

    backgroundBorder.setShape(shape);
    backgroundBorder.getPaint().setColor(colorBorderNormal);
    backgroundBorder.getPaint().setStyle(Style.STROKE);
    backgroundBorder.getPaint().setStrokeWidth(defaultStrokeWidth);
    backgroundBorder.getPaint().setAntiAlias(true);

    layers[0] = background;
    layers[1] = backgroundBorder;

    return new LayerDrawable(layers);
}

【讨论】:

    【解决方案2】:

    这个对我有用,给了我圆形的颜色

     ShapeDrawable biggerCircle= new ShapeDrawable( new OvalShape());
                    biggerCircle.setIntrinsicHeight( 60 );
                    biggerCircle.setIntrinsicWidth( 60);
                    biggerCircle.setBounds(new Rect(30, 30, 30, 30));
                    biggerCircle.getPaint().setColor(Color.parseColor(first));//you can give any color here
                    holder.firstcolor.setBackgroundDrawable(biggerCircle); 
    

    【讨论】:

    • 谢谢。如果使用 RoundRectShape ,您的代码对我有用。!! ShapeDrawable largeCircle = new ShapeDrawable(new RoundRectShape(...));
    • @jihoonkim 很高兴为您提供帮助..快乐编码:)
    【解决方案3】:

    首先使用 getbackground() 获取 Drawable。

    Drawable background = imageView.getBackground();
    
    
    if (background instanceof ShapeDrawable) {
        // cast to 'ShapeDrawable'
        ShapeDrawable shapeDrawable = (ShapeDrawable)background;
        shapeDrawable.getPaint().setColor(getResources().getColor(R.color.colorToSet));
    } else if (background instanceof GradientDrawable) {
        // cast to 'GradientDrawable'
        GradientDrawable gradientDrawable = (GradientDrawable)background;
        gradientDrawable.setColor(getResources().getColor(R.color.colorToSet));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-31
      • 2016-10-11
      • 1970-01-01
      • 2015-11-16
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多