How to add a stroke to ShapeDrawable

Hi I want to create a form that can be aligned and fill it with a gradient color with a 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 that the rectangle appears with a gradient fill, but without a stroke

+4
source share
4 answers

This person seemed to be struggling with the same problem, and the only way they discovered was the subclassical ShapeDrawable:

Trying to draw a button: how to set the color of the stroke and how to "align" the gradient to the bottom, not knowing the height?

0
source

ShapeDrawable does not make it easy to draw a stroke around it. If you really want to, then this will be a nice place to browse.

OR

You can use GradientDrawable

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

(PS: This is indicated as a comment on the same page!)

+10
source
 <stroke android:width="2dp" android:color="#808080" /> 

try it, it will definitely work

0
source

Change the style to greenShape.getPaint (). setStyle (Paint.Style.STROKE) and

 greenShape.setShaderFactory(new ShapeDrawable.ShaderFactory() { @Override public Shader resize(int width, int height) { return new LinearGradient(0, 0, 0, 0, Color.WHITE, Color.WHITE, Shader.TileMode.REPEAT); 
0
source

All Articles