我想画一个网格。所以我有
private int GRID_WIDTH = 6; <----Amount of columns
private int GRID_HEIGHT = 6; <----Amount of rows
private int GRID_SIZE; = 50 <----Width and height of a cell
现在我试图绘制它们:
for(int i = 0; i < GRID_WIDTH; i++) {
for(int j = 0; j < GRID_HEIGHT; j++) {
canvas.drawRect(new Rect(i*GRID_SIZE + 5, j*GRID_SIZE + 5, GRID_SIZE, GRID_SIZE), paint);
}
}
每个坐标后面的“5”应该在两个矩形之间产生间隙。 这应该在一些漂亮的网格中结束,但结果我看到多个矩形被推到一起,它们之间没有这些5px填充。无论我选择什么作为填充,它都会产生以下图像:(这里填充设置为20而不是5 ...)
我做错了什么?
提前致谢!
答案 0 :(得分:3)
考虑Rect构造函数签名是:
Rect(int left, int top, int right, int bottom)
你做得像:
Rect(int left, int top, int width, int height)
注意最后两个参数的区别。你必须这样做:
int left = i * (GRID_SIZE + 5);
int top = j * (GRID_SIZE + 5);
int right = left + GRID_SIZE;
int bottom = top + GRID_SIZE;
canvas.drawRect(new Rect(left, top, right, bottom), paint);