31

I`m trying to draw custom ShapeDrawable with OvalShape, filled with white and with grey border. I created a drawable like this:

ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.GRAY);
drawable.getPaint().setStyle(Style.STROKE);
drawable.getPaint().setStrokeWidth(getPixels(5));
drawable.getPaint().setAntiAlias(true);

But the result of that was: corners problem

enter image description here

The idea is programmatically to create a shape like this but with different colors:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>

How can can be fix this?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
L3K0V
  • 1,104
  • 1
  • 9
  • 24
  • Are you saying that the problem is the clipping of the border shown in that linked image? – Todd Sjolander Mar 06 '13 at 12:55
  • Yes! There is no problem if stroke width is 0 (hairline) but not a solution for me. The method getPixels() return pixels for given dip by the way... but i think isn`t the problem. – L3K0V Mar 06 '13 at 13:07
  • I think the problem is the size of your drawable. You need to decrease the size enough to fit the width of the border. – Todd Sjolander Mar 06 '13 at 13:35
  • By the way the documentation says that borders apply only to rectangles, not ovals. – rkapl Mar 06 '13 at 13:36
  • Then is there a simply way to create a border to oval, and when stroke width is 0 there`s border but super-mega tiny... – L3K0V Mar 06 '13 at 16:35
  • 1
    Can see this for perfect solution of clip http://stackoverflow.com/questions/18693721/oval-shape-clipped-when-created-programmatically?rq=1 – Akhil Dad Oct 15 '14 at 06:49
  • Just add padding using drawable.setPadding(4,4,4,4) – Rahul Mandaliya Feb 03 '17 at 12:12

2 Answers2

47

I found a way to get around creating of new drawables!

A defined a circle with border from android XML as follow:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="oval">
<corners android:radius="10dip"/>
<stroke android:color="#FF0000" android:width="5dip"/>
<solid android:color="@android:color/transparent"/>
</shape>

Then when a want to change drawable color, i'm applying ColorFilter. For example if I want to change drawable's red color to blue i do this:

Drawable drawable = context.getResources().getDrawable(R.drawable.name);
drawable.setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_ATOP);

If we want to use StateListDrawable for creating custom selectors from code be aware - StateListDrawable clears applied to drawables filters... apply filter on whole selector by selector.setColorFilter... fix the problem.

patridge
  • 26,385
  • 18
  • 89
  • 135
L3K0V
  • 1,104
  • 1
  • 9
  • 24
  • I'm running into a problem: it's not possible to do `LinearLayout.addView(drawable, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));` – Someone Somewhere Oct 22 '13 at 06:55
  • You are trying to add drawable to layout instead of view... Create a image view or image button then set drawable to the view before add it to layout and off course add the to the layout. – L3K0V Oct 23 '13 at 09:11
  • Do you know how I could change the background color programmatically ? – Simon Rolin Dec 18 '13 at 13:43
  • 2
    It was as easy as this : GradientDrawable bgShape = (GradientDrawable) btn.getBackground(); bgShape.setColor(Color.BLACK); – Simon Rolin Dec 18 '13 at 13:48
  • Does this update the `stroke` color or the `solid` color? – tir38 Oct 12 '15 at 20:28
  • In this case - only `stroke` color, because I set solid to transparent. – L3K0V Nov 13 '15 at 14:04
1

this is a new solution:

 RoundedBitmapDrawable RBD = RoundedBitmapDrawableFactory.create(mContext.getResources(),YourBitmap);
            RBD.setCornerRadius(20);
            RBD.setAntiAlias(true);

            wholeRel.setBackground(RBD);
BabakHSL
  • 622
  • 1
  • 8
  • 18