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

Posted

技术标签:

【中文标题】如何以编程方式创建 android 形状背景?【英文标题】:How to create android shape background programmatically? 【发布时间】:2015-04-19 03:56:23 【问题描述】:

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

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

但我得到了这个错误:

没有为LinearLayout类型定义getDrawable()方法

【问题讨论】:

见post 或this... 为什么不使用多种样式? @Droidekas 如果我无法创建自定义函数来创建它,我必须拥有超过 20 个样式的 xml 文件 您尝试过使用自定义视图吗? @Droidekas 不,先生,您能介绍一些教程或帮助我吗?顺便说一下更新后 【参考方案1】:

你可以这样做:

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 参数的含义。

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

【讨论】:

有帮助...解决了我的问题...谢谢,但不推荐使用 setBackgroundDrawable :( @AnkushJoshi,使用 setBackground 太棒了!谢谢。 像魅力一样工作。谢谢,+1【参考方案2】:

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

    float r=8;
    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));

【讨论】:

【参考方案3】:

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

请看这里: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();

【讨论】:

【参考方案4】:

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

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

【讨论】:

【参考方案5】:

如果你想创建

带渐变的圆形可绘制对象

然后使用下面的代码。

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;

【讨论】:

以上是关于如何以编程方式创建 android 形状背景?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式更改背景颜色而不影响边框?

以编程方式更改图层列表中形状的颜色

LayerDrawable 以编程方式

如何在Android中以编程方式向影像添加阴影? [重复]

以编程方式创建形状视图并在集合视图单元格上使用

如何在 Android 中以编程方式设置背景可绘制对象