Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a stroke to ShapeDrawable

Hi I want to create a shape drawable and fill it with gradient color with white stroke here is my code

    ShapeDrawable greenShape = new ShapeDrawable(new RectShape());
    Shader shader1 = new LinearGradient(0, 0, 0, 50, new int[] {
            0xFFBAF706, 0xFF4CD52F  }, null, Shader.TileMode.CLAMP);
    greenShape.getPaint().setShader(shader1);
    greenShape.getPaint().setStrokeWidth(3);
    greenShape.getPaint().setColor(Color.WHITE);
    greenShape.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);`

The problem is the rectangle appears with gradient fill but without stroke

like image 703
JustMe Avatar asked Jun 03 '11 19:06

JustMe


People also ask

What is stroke width android?

The stroke-width attribute is a presentation attribute defining the width of the stroke to be applied to the shape.

How do I change the stroke color on my Android?

To set the shape stroke color programmatically this example uses GradientDrawable method setStroke(int width, int color) which sets the stroke width and change shape's color. Here default orange color of the shape drawable is changes programetically usingb GradientDrawable method setStroke(int width, int color).

What is layerlist in android?

Layer list. A LayerDrawable is a drawable object that manages an array of other drawables. Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top. Each drawable is represented by an <item> element inside a single <layer-list> element.


1 Answers

The ShapeDrawable does not allow you to easily draw a stroke around it. If you really want then this would be a nice place to look at.

OR

You could use a GradientDrawable

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);

(PS: This is given as a comment in the same page!)

like image 51
Sree Aurovindh Avatar answered Sep 19 '22 02:09

Sree Aurovindh