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

40,590

Solution 1

this is a new solution:

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

            wholeRel.setBackground(RBD);

Solution 2

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.

Share:
40,590
L3K0V
Author by

L3K0V

Experienced native mobile and web developer with demonstrated history of working on projects from their beginning till release and after that supporting them. Participated in making important decisions during the research and development phases. Skilled in Android, Ruby on Rails, Django, Architecture and concept design. Interested in education with a solid experience behind teaching Android to undergraduate and Programming in C to high-school students.

Updated on December 13, 2020

Comments

  • L3K0V
    L3K0V over 3 years

    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?

  • Someone Somewhere
    Someone Somewhere over 10 years
    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));
  • L3K0V
    L3K0V over 10 years
    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.
  • Simon Rolin
    Simon Rolin over 10 years
    Do you know how I could change the background color programmatically ?
  • Simon Rolin
    Simon Rolin over 10 years
    It was as easy as this : GradientDrawable bgShape = (GradientDrawable) btn.getBackground(); bgShape.setColor(Color.BLACK);
  • tir38
    tir38 over 8 years
    Does this update the stroke color or the solid color?
  • L3K0V
    L3K0V over 8 years
    In this case - only stroke color, because I set solid to transparent.