Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically add Gradient with solid color and stroke

Currently I am using this code to add color:

ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(color);

Now I need to apply some gradient colors to it along with stroke(like border with different color). I am setting this as background to button.

Here is what I am expecting, I need to do it programmatically.

enter image description here

like image 327
Goofy Avatar asked Jul 07 '26 22:07

Goofy


1 Answers

Add a RadialGradient to your drawable like this:

Shader shader = new RadialGradient(x, y, radius, color0, color1, Shader.TileMode.REPEAT);
drawable.getPaint().setShader(shader);

Obviously, you can interchange LinearGradient, SweepGradient, and any of the parameters.

Here is how to add the stroke:

drawable.getPaint().setStrokeWidth(3);
drawable.getPaint().setColor(Color.WHITE);
drawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);

Hmmm, I think I have to defer to @StinePike with the GradientDrawable:

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
gd.setShape(GradientDrawable.OVAL);
like image 101
David Manpearl Avatar answered Jul 11 '26 07:07

David Manpearl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!