删除 android 中矩形边上的部分线条

Remove part of lines in sides of rectange in android

我想为 Android 中的视图绘制一个背景矩形。这是我的代码

rectBox= new RectF(0, 0, 200,
        200);
Paint paint= new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(2);
paint.setColor(Color.BLACK);
canvas.drawRoundRect(rectBox, 0, 0, paint);

我正在获取输出

我想要这个输出。我想删除矩形边缘的部分线段

请告诉我怎么做?

只需在该段上绘制带有背景颜色的线条(或矩形)。

更新 使用 drawLine()drawArc().

仅绘制必要的部分

您可以在必要的地方画透明线。

Paint paint = new Paint();
paint.setColor(Color.TRANSPARENT);
canvas.drawLine(0, 8, 0, 15, paint);
canvas.drawLine(8, 0, 15, 0, paint);
canvas.drawLine(8, 8, 8, 15, paint);
canvas.drawLine(15, 8, 15, 15, paint);

我不确定圆角,但这是我在我的一个项目中所做的

public void draw(@NonNull Canvas canvas) {
    super.draw(canvas);

        // Full left band
        canvas.drawRect(0, 0, rect.left, getHeight(), rectPaint);

        // Full right band
        canvas.drawRect(rect.right, 0, getWidth(), getHeight(), rectPaint);

        // Partial top band
        canvas.drawRect(rect.left, 0, rect.right, rect.top, rectPaint);

        // Partial bottom band
        canvas.drawRect(rect.left, rect.bottom, rect.right, getHeight(), rectPaint);

        // Top lines
        canvas.drawRect(
                rect.left - strokeWidth, rect.top - strokeWidth,
                rect.left + cornerLength, rect.top,
                borderPaint
        );
        canvas.drawRect(
                rect.right - cornerLength, rect.top - strokeWidth,
                rect.right + strokeWidth, rect.top,
                borderPaint
        );

        // Bottom lines
        canvas.drawRect(
                rect.left - strokeWidth, rect.bottom,
                rect.left + cornerLength, rect.bottom + strokeWidth,
                borderPaint
        );
        canvas.drawRect(
                rect.right - cornerLength, rect.bottom,
                rect.right + strokeWidth, rect.bottom + strokeWidth,
                borderPaint
        );

        // Left lines
        canvas.drawRect(
                rect.left - strokeWidth, rect.top - strokeWidth,
                rect.left, rect.top + cornerLength,
                borderPaint
        );
        canvas.drawRect(
                rect.left - strokeWidth, rect.bottom - cornerLength,
                rect.left, rect.bottom + strokeWidth,
                borderPaint
        );

        // Right lines
        canvas.drawRect(
                rect.right, rect.top - strokeWidth,
                rect.right + strokeWidth, rect.top + cornerLength,
                borderPaint
        );
        canvas.drawRect(
                rect.right, rect.bottom - cornerLength,
                rect.right + strokeWidth, rect.bottom + strokeWidth,
                borderPaint
        );

}