[英]How paint small rectangels on a canvas on Android?
I would like to draw small rectangles on a Canvas. 我想在画布上绘制小矩形。 The problem is that the rectangles don´t get any border, so the result is just a big rectangle (containing all the small rectangles (20x20 rectangles as stated below)).
问题在于矩形没有任何边界,因此结果只是一个大矩形(包含所有小矩形(如下所述的20x20矩形))。
In my custom View
, I override onDraw: 在我的自定义
View
,我重写了onDraw:
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawRect(getWidth(), getHeight(), 50, 50, paint);
paint.setColor(Color.LTGRAY);
paint.setStrokeWidth(3);
for(int i = 0; i < 20; i++) {
for(int j = 0; j < 20; j++) {
canvas.drawRect(20*j, 20*i, 50, 50, paint);
}
}
}
For Java Swing, you can just do 对于Java Swing,您可以执行以下操作
g.setColor(Color.red);
for(int x = 0; x < 20; x++) {
for(int y = 0; y < 20; y++) {
g.drawRect(20 * x, 20 * y, 20, 20);
}
}
to paint the rectangles and each rectangle will have red borders. 绘制矩形,每个矩形都有红色边框。
How can I achieve this on Android? 如何在Android上实现此目标?
I guess you need to change paint style to Stroke before drawing small rectangles. 我猜您需要在绘制小矩形之前将绘画样式更改为“描边”。 Use
mPaint.setStyle(Paint.Style.STROKE)
along with setting stroke width. 将
mPaint.setStyle(Paint.Style.STROKE)
与设置笔触宽度一起使用。 In your code you haven't declared paint style anywhere. 在您的代码中,您没有在任何地方声明绘画风格。 If you use
Paint.Style.FILL
it will fill the rectangle. 如果使用
Paint.Style.FILL
,它将填充矩形。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.