Draw a square with rounded corners.

I can draw a rectangle with sharp edges, now I need to make the sharp edges rounded.

How to do it?

This is my code:

 public void drawShape(Canvas canvas, Renderer renderer, float x, float y,
      int seriesIndex, Paint paint) {
    float halfShapeWidth = shape_width / 2;
    canvas.drawRect(x , y - halfShapeWidth, x + SHAPE_WIDTH, y + halfShapeWidth, paint);
   }

How to make this rounded rectangle by passing the same parameters?

+5
source share
3 answers

Ok, I solved it myself using this code:

RectF r = new RectF(1,2,3,4);
canvas.drawRoundRect(r, 0, 0, mPaint);

Hope this helps others.

+27
source

You can use drawRoundRect

You will need to package the position and dimensions in RectF before you can call this function.

+6
source
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
-5
source

All Articles