我想在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);
我正在获取输出
我想要这个输出。我想删除矩形边缘中的部分片段
请说明如何做到这一点?
答案 0 :(得分:0)
只需在这些线段上绘制背景颜色的线条(或矩形)。
<强>更新强>
使用drawLine()
和drawArc()
。
答案 1 :(得分:0)
您可以在必要的地方绘制透明线条。
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);
答案 2 :(得分:0)
我不确定圆角但是我在我的一个项目中放弃了什么
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
);
}