Android 如何创建具有两个圆角的矩形?

Android 如何创建具有两个圆角的矩形?,android,android-canvas,rect,android-paint,Android,Android Canvas,Rect,Android Paint,我需要使用画布创建一个具有两个圆角的矩形视图。我使用了drawRoundRect,但我得到的是具有四个圆角的矩形。请任何人向我建议一种有助于解决我的问题的方法 rect = new RectF(left, top, right, bottom); canvas.drawRoundRect(rect, 20, 20, facePaint); 没有内置的函数可以做到这一点。您需要创建一个具有所需形状的路径,然后使用drawPath将路径绘制到画布只需转到您的drawable文件夹添加新的xm

我需要使用画布创建一个具有两个圆角的矩形视图。我使用了drawRoundRect,但我得到的是具有四个圆角的矩形。请任何人向我建议一种有助于解决我的问题的方法

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

 canvas.drawRoundRect(rect, 20, 20, facePaint);

没有内置的函数可以做到这一点。您需要创建一个具有所需形状的路径,然后使用drawPath将路径绘制到画布

只需转到您的drawable文件夹添加新的xml文件并添加以下代码:-

<?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> 
你可以根据自己的选择改变颜色

希望它能对您有所帮助。

请使用:-

// 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);
//初始化新位图
位图位图=位图.createBitmap(
600,//宽度
300,//高度
Bitmap.Config.ARGB_8888//Config
);
//初始化一个新的画布实例
画布=新画布(位图);
//在画布上绘制纯色作为背景
画布。drawColor(颜色。白色);
//初始化新的绘制实例以绘制圆角矩形
油漆=新油漆();
绘制.设置样式(绘制.样式.填充);
油漆。设置颜色(颜色。红色);
paint.setAntiAlias(真);
//设置偏移值(以像素为单位),以便在画布上绘制圆角矩形
整数偏移=50;
/*
公共RectF(左侧浮动、顶部浮动、右侧浮动、底部浮动)
创建具有指定坐标的新矩形。注:无范围
已执行检查,因此调用方必须确保

左是
drawRoundRect
的源代码。因此,如果要绘制两个圆角

public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
你可以试试这个。左上角有圆角

canvas.drawRoundRect(10,10,0,0,20,20,facePaint);

有点浪费,但首先画一个圆角矩形,四个角都是圆角,然后在下面画第二个规则矩形
半径
高度,在底部圆角上画

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);
}

需要一点数学知识来解释所需的高度,但不应该那么难:)

您可以使用xml绘制两个圆角矩形

<?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>


只需添加一个填充矩形(或两个填充正方形-如果不需要的角对角相对)来“覆盖”不需要的圆角。但是OP想要使用画布对象。您也可以使用GradientDrawable gd=new GradientDrawable();gd.setColor(Color.RED);gd.设置转弯半径(10);gd.设定行程(2,颜色为白色);视图.倒退(gd);使用视图变量代替视图。确定。但是OP想要使用画布对象,它不是“给我的”。它用于OP。使用视图名称代替imageview。如果你得到帮助也要投票。
<?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>