Goofy
Goofy

Reputation: 6128

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

Upvotes: 3

Views: 6540

Answers (2)

David Manpearl
David Manpearl

Reputation: 12656

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

Upvotes: 4

stinepike
stinepike

Reputation: 54742

use GradientDrawable to create gradient

or

you can see this answer

Upvotes: 3

Related Questions