How to create android shape background programmatically?

前端 未结 5 1693
无人共我
无人共我 2020-12-13 03:35

How to create this shape programmatically?




        
相关标签:
5条回答
  • 2020-12-13 04:14

    I've created a library which can help to create drawables programmatically.

    See here: DrawableToolbox.

    With DrawableToolbox, you can create it by:

    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();
    
    0 讨论(0)
  • 2020-12-13 04:16

    If you want to Create

    Rounded drawable with Gradient

    then use below code.

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 04:20

    You can do it like this:

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

    See the documentation for the meaning of setCornerRadii params.

    You can use this function throughout your app and can put border and background color of your choice.

    0 讨论(0)
  • 2020-12-13 04:20

    You can also use OVAL shape instead of rectangle:

    GradientDrawable shape = new GradientDrawable();
    shape.setShape(GradientDrawable.OVAL);
    shape.setColor(Color.WHITE);
    shape.setStroke(2, Color.BLACK);
    view.setBackground(shape);
    
    0 讨论(0)
  • 2020-12-13 04:21

    If what you want is just a simple rounded rectangle, cut the long story short.

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



    • What is the RoundRectShape ?

    RoundRectShape specifies an outer (round) rect and an optional inner (round) rect.

    // RoundRectShape constructor
    
       RoundRectShape(float[] outerRadii,
                         RectF inset,
                       float[] innerRadii);
    
    • outerRadii is an array of 8 radius values, for the outer roundrect. The first two floats are for the top-left corner (remaining pairs correspond clockwise). For no rounded corners on the outer rectangle, just pass null.

    For example:

    • inset is a RectF that specifies the distance from the inner rect to each side of the outer rect. For no inner, pass null.

    • innerRadii is an array of 8 radius values, for the inner roundrect. The first two floats are for the top-left corner (remaining pairs correspond clockwise). For no rounded corners on the inner rectangle, pass null. If inset parameter is null, this parameter is ignored.

    For example:

    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}));
    
    0 讨论(0)
提交回复
热议问题