2

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

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

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

6 回答 6

1

有点浪费,但首先绘制一个圆角矩形,所有 4 个角都是圆形的,然后radius在下面绘制第二个常规矩形高度以覆盖底部圆角

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

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

于 2017-11-17T10:53:22.460 回答
0

只需转到您的可绘制文件夹添加新的 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> 

在这样的任何视图的设置背景之后: -

android:background="@drawable/round_green"

你可以根据你的选择改变颜色。

希望它会帮助你。

于 2017-11-17T09:50:52.923 回答
0

您可以使用 xml drawable 制作 2 个角的圆角矩形。

<?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>
于 2019-08-01T12:05:29.953 回答
0

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

于 2017-11-17T09:43:24.487 回答
0

用这个 :-

// 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);
于 2017-11-17T10:19:54.377 回答
0

的来源。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);
于 2017-11-17T10:22:57.717 回答