2

I need to create a rectangle view with two rounded corners using canvas.I used drawRoundRect but i am getting rectangle with four rounded corners.Please anyone suggest me a way that can be helpful in solving my problem.

 rect = new RectF(left, top, right, bottom);

 canvas.drawRoundRect(rect, 20, 20, facePaint);
Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
  • Just add a filled rectangle (or 2 filled squares - if the unwanted corners are diagonally opposed) to "cover" the unwanted rounded corners. – Phantômaxx Nov 17 '17 at 10:04

6 Answers6

1

A bit wasteful but first draw a rounded rectangle with all 4 corners rounded, then draw second, regular rectangle radius height below to draw over the bottom rounded corners

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
float radius = 20f;

@Override
public void onDraw(Canvas canvas) {
    // Draw the top part that has rounded corners with twice the height of the radius
    canvas.drawRoundRect(0f, 0f, 100f, 2 * radius, radius, radius, paint);
    // Draw the bottom part, partly on top of the top part
    canvas.drawRect(0f, radius, 100f, 100f, paint);
}

A tiny bit of math needed to account for the desired height but shouldn't be that hard :)

patrick.elmquist
  • 1,957
  • 2
  • 18
  • 30
0

There is no built in function to do that. You'll need to create a Path with the shape you want, then use drawPath to draw the path to the canvas

Gabe Sechan
  • 77,740
  • 9
  • 79
  • 113
0

Simply go to you drawable folder add new xml file and add below code:-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners
      android:radius="20dp"/>
    <solid android:color="@color/green" />
</shape> 

After that set background of any view like this:-

android:background="@drawable/round_green"

you can change color according to your choice.

Hope it will helps you.

0

Use this :-

// Initialize a new Bitmap
                Bitmap bitmap = Bitmap.createBitmap(
                        600, // Width
                        300, // Height
                        Bitmap.Config.ARGB_8888 // Config
                );

                // Initialize a new Canvas instance
                Canvas canvas = new Canvas(bitmap);

                // Draw a solid color on the canvas as background
                canvas.drawColor(Color.WHITE);

                // Initialize a new Paint instance to draw the rounded rectangle
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.FILL);
                paint.setColor(Color.RED);
                paint.setAntiAlias(true);

                // Set an offset value in pixels to draw rounded rectangle on canvas
                int offset = 50;

                /*
                    public RectF (float left, float top, float right, float bottom)
                        Create a new rectangle with the specified coordinates. Note: no range
                        checking is performed, so the caller must ensure that
                        left <= right and top <= bottom.

                    Parameters
                        left  The X coordinate of the left side of the rectangle
                        top  The Y coordinate of the top of the rectangle
                        right  The X coordinate of the right side of the rectangle
                        bottom  The Y coordinate of the bottom of the rectangle
                */
                // Initialize a new RectF instance
                RectF rectF = new RectF(
                        offset, // left
                        offset, // top
                        canvas.getWidth() - offset, // right
                        canvas.getHeight() - offset // bottom
                );

                /*
                    public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
                        Draw the specified round-rect using the specified paint. The roundrect
                        will be filled or framed based on the Style in the paint.

                    Parameters
                        rect : The rectangular bounds of the roundRect to be drawn
                        rx : The x-radius of the oval used to round the corners
                        ry : The y-radius of the oval used to round the corners
                        paint : The paint used to draw the roundRect
                */

                // Define the corners radius of rounded rectangle
                int cornersRadius = 25;

                // Finally, draw the rounded corners rectangle object on the canvas
                canvas.drawRoundRect(
                        rectF, // rect
                        cornersRadius, // rx
                        cornersRadius, // ry
                        paint // Paint
                );

                // Display the newly created bitmap on app interface
                mImageView.setImageBitmap(bitmap);
0

The source of drawRoundRect .So if you want to draw two round corners .

public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)

And you can try this .Just left and top have round corners.

canvas.drawRoundRect(10,10,0,0,20,20,facePaint);
KeLiuyue
  • 7,458
  • 4
  • 19
  • 35
0

You can make 2 corners rounded rectangle with xml drawable.

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:bottomLeftRadius="30dp" android:bottomRightRadius="30dp"></corners>
    <solid android:color="@color/white"></solid>
</shape>
Savas Adar
  • 3,357
  • 2
  • 38
  • 48