如何以编程方式创建 android 形状背景?

How to create android shape background programmatically?

如何以编程方式创建此形状?

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

    <solid android:color="#e67e22"/> 
    <corners
        android:topLeftRadius="0dp"
        android:topRightRadius="0dp"
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"/>
</shape>

我试过这个简单的函数,它获取角、颜色并将其设置为形状:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.category_header);

    GradientDrawable drawable = (GradientDrawable) linearLayout.getDrawable();

    float[] values = { 0.2f, 0.2f, 0.2f, 0.2f };
    drawable.setCornerRadii(values);

但是我得到了这个错误:

方法 getDrawable() 未定义类型 LinearLayout

你可以这样做:

public static void customView(View v, int backgroundColor, int borderColor) {
    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.RECTANGLE);
    shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
    shape.setColor(backgroundColor);
    shape.setStroke(3, borderColor);
    v.setBackground(shape);
}

请参阅 documentation 了解 setCornerRadii 参数的含义。

您可以在整个应用程序中使用此功能,并可以放置您选择的边框和背景颜色。

您也可以使用 OVAL 形状代替矩形:

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.OVAL);
shape.setColor(Color.WHITE);
shape.setStroke(2, Color.BLACK);
view.setBackground(shape);

我创建了一个库,可以帮助以编程方式创建可绘制对象。

看这里:DrawableToolbox.

使用 DrawableToolbox,您可以通过以下方式创建它:

Drawable drawable = new DrawableBuilder()
        .rectangle()
        .solidColor(0xffe67e22)
        .bottomLeftRadius(20) // in pixels
        .bottomRightRadius(20) // in pixels
//        .cornerRadii(0, 0, 20, 20) // the same as the two lines above
        .build();

如果你想创建

Rounded drawable with Gradient

然后使用下面的代码。

public static GradientDrawable generateGradientBackgroundCircular(String topColor, String bottomColor) {
    int[] colors = {Color.parseColor(topColor), Color.parseColor(bottomColor)};

    //create a new gradient color
    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TL_BR, colors);
    gd.setShape(GradientDrawable.OVAL);

    return gd;
}

如果您想要的只是一个简单的圆角矩形,长话短说。

    float r=8; // the border radius in pixel
    ShapeDrawable shape = new ShapeDrawable (new RoundRectShape(new float[] { r, r, r, r, r, r, r, r },null,null));
    shape.getPaint().setColor(Color.RED);
    view.setBackground(shape);



  • 什么是 RoundRectShape?

RoundRectShape 指定一个外部 (round) 矩形和一个可选的内部 (round) 矩形。

// RoundRectShape constructor

   RoundRectShape(float[] outerRadii,
                     RectF inset,
                   float[] innerRadii);
  • outerRadii 是一个包含 8 个半径值的数组,用于外圆角。 前两个浮点数用于 左上角 角(其余对顺时针对应)。对于外部矩形上没有圆角,只需传递 null.

例如:

  • inset 是一个 RectF 指定距离内部矩形到外部矩形的每一侧。对于没有内层的,传null.

  • innerRadii 是一个包含 8 个半径值的数组,用于内圆角。前两个浮点数用于左上角(其余对顺时针对应)。对于内部矩形上没有圆角,传递 null。如果 inset 参数为 null,则忽略此参数。

例如:

ShapeDrawable shape = new ShapeDrawable(
        new RoundRectShape(
            new float[]{20, 20, 20, 20, 20, 20, 20, 20},
            new RectF(10, 20, 10, 20),
            new float[]{40, 40, 40, 40, 40, 40, 40, 40}));