Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing programmatically oval shape with border (corner radius) on Android

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?

like image 389
L3K0V Avatar asked Mar 06 '13 12:03

L3K0V


2 Answers

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.

like image 67
L3K0V Avatar answered Sep 20 '22 01:09

L3K0V


this is a new solution:

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

            wholeRel.setBackground(RBD);
like image 1
BabakHSL Avatar answered Sep 22 '22 01:09

BabakHSL