3

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Goofy
  • 6,098
  • 17
  • 90
  • 156

2 Answers2

4

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);
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • Thanks what about stroke which i have it in my image? i mean the blue border – Goofy Mar 22 '13 at 06:16
  • No buddy its not working and corners are rectangle now and also i want the stroke color to be different, can you please help me on that – Goofy Mar 22 '13 at 06:19
  • That's all I got. When I saw your request I was expecting my RadialGradient example from a recent project to work for you. Without a bit of experimenting, I cannot help you any further. I hope this helped a little bit. Good luck! – David Manpearl Mar 22 '13 at 06:26
  • One last and final question i tried this :GradientDrawable drawable = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{Color.RED, Color.GREEN}); but i need the CENTER instead of TOP_BOTTOM – Goofy Mar 22 '13 at 06:48
3

use GradientDrawable to create gradient

or

you can see this answer

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112