2014-02-20 106 views
2

我想画一个网格。所以我有试图绘制网格结束绘图长矩形

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之间的填充。无论我选择作为填充,它resuls在下面的图片:(这里的填充设置为20,而不是5 ...)

enter image description here

我到底做错了什么?

在此先感谢!

回答

3

认为矩形构造函数签名是:

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); 
+0

int left = i * GRID_SIZE + 5; 没有工作,但是 int left = i *(GRID_SIZE + 5); did 感谢您的回答! – user2410644

+0

与top = j * GRID_SIZE + 5相同。使用top = j *(GRID_SIZE + 5)。 – AnkitRox

+0

谢谢!固定。 =) –