0

我想在画布上绘制小矩形。问题是矩形没有任何边框,所以结果只是一个大矩形(包含所有小矩形(如下所述的 20x20 矩形))。

在我的自定义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);
        }
    }
} 

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

绘制矩形,每个矩形都有红色边框。

如何在 Android 上实现这一点?

4

1 回答 1

0

我想您需要在绘制小矩形之前将绘制样式更改为 Stroke。mPaint.setStyle(Paint.Style.STROKE)与设置笔画宽度一起使用。在您的代码中,您没有在任何地方声明绘制样式。如果你使用Paint.Style.FILL它将填充矩形。

于 2014-02-28T16:19:19.533 回答