20

I need to create a border with rounded corners programatically by extending ShapeDrawable. I need to have a black border with rounded corners with the pixels on the outside being white and the inner pixels being transparent. The code I have at the moment has multiple problems, of which are that it does not create a smooth corner that is the same thickness as the border and that the outer pixels of the border are transparent and not white.

Here is a picture of the corners I am currently getting corner

Here is the code where I am passing Color.TRANSPARENT for 'fill' in the constructor:

public class CustomShape extends ShapeDrawable {
 private final Paint fillpaint, strokepaint;
public CustomShape(int fill, int strokeWidth,int radius) {

    super(new RoundRectShape(new float[] { radius, radius, radius, radius, radius, radius, radius, radius }, null, null));
    fillpaint = new Paint(this.getPaint());
    fillpaint.setColor(fill);
    strokepaint = new Paint(fillpaint);
    strokepaint.setStyle(Paint.Style.STROKE);
    strokepaint.setStrokeWidth(strokeWidth);
    strokepaint.setColor(Color.BLACK);
}



@Override
protected void onDraw(Shape shape, Canvas canvas, Paint paint) {
    shape.draw(canvas, fillpaint);
    shape.draw(canvas, strokepaint);
}

}

user1592512
  • 423
  • 2
  • 4
  • 7

6 Answers6

30

If you need evenly rounded corners (and from your example it seems you do) you can simply use GradentDrawable with a solid color

GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);

view.setBackground(gd);

GradientDrawable documentation can be found here.

Edit: For each corner separately

You can specify radius of each corner separately using setCornerRadii (float[] radii) method. "For each corner, the array contains 2 values, [X_radius, Y_radius]. The corners are ordered top-left, top-right, bottom-right, bottom-left. This property is honored only when the shape is of type RECTANGLE.

It is recommended to invoke mutate() before changing this property.

Community
  • 1
  • 1
SGal
  • 1,072
  • 12
  • 13
1

Instead of GradientDrawable, its better to use ShapeDrawable with RoundRectShape, here is an example of it:

// individualRoundedCorners
val roundCorners = floatArrayOf(
    topLeftRadius, topLeftRadius,
    topRightRadius, topRightRadius,
    bottomRightRadius, bottomRightRadius,
    bottomLeftRadius, bottomLeftRadius
)

or

// similarCornerRadius
val roundCorners = FloatArray(8) { cornerRadius }

then the drawable itself

val shapeDrawable = ShapeDrawable().apply {
        shape = RoundRectShape(roundCorners, null, null)
        paint.color = Color.RED
    }

more about RoundRectShape

Akbolat SSS
  • 1,761
  • 15
  • 23
0

You can implement a custom drawable. Following is the example of the xml.

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#ffffffff"/>    

    <stroke android:width="3dp"
            android:color="#ff000000"
            />

    <padding android:left="1dp"
             android:top="1dp"
             android:right="1dp"
             android:bottom="1dp"
             /> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape>

Save this xml in the drawable folder of your project. Now use it like a normal drawable for any widget. For example: android:background="R.drawable.round_shape"

This example is referenced following link.

Community
  • 1
  • 1
muneikh
  • 2,067
  • 5
  • 25
  • 59
0

In addition to specify the round corner dimens you can use GradientDrawable and the method setCornerRadii()

GradientDrawable d = new GradientDrawable();
d.setCornerRadii({5.0f,5.0f,5.0f,5.0f});
textViewExample.setBackgroundResource(d);
0
GradientDrawable drawable = (GradientDrawable)image.getBackground();
drawable.setGradientRadius(radiuspx);
inhogo
  • 253
  • 3
  • 9
0
 setCornerRadii(new float[] {
                    topLeftRadius, topLeftRadius,
                    topRightRadius, topRightRadius,
                    bottomRightRadius, bottomRightRadius,
                    bottomLeftRadius, bottomLeftRadius
            });