繁体   English   中英

如何以编程方式创建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);
}

有关setCornerRadii参数的含义,请参阅文档

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

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

    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指定一个外部(圆形)矩形和一个可选的内部(圆形)矩形。

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

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

请参见此处: 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();

您还可以使用OVAL形状而不是矩形:

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

如果你想创建

带渐变的圆形可绘制

然后使用下面的代码。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM