我想画一个网格。因此我有
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...)
我做错了什么?
提前致谢!
最佳答案
考虑 Rect 构造函数签名是:
矩形(int 左,int 上,int 右,int 下)
你正在这样做:
矩形(int左,int顶部,int宽度,int高度)
请注意最后两个参数的差异。你必须这样做:
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);
关于android - 尝试绘制网格最终绘制长矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921181/