我想在我的ui上画一个矩形矩阵。问题是只有一个矩形,即矩阵的第一个矩形,可见。
这是我的代码:
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.GRAY);
if (w != 0 && h != 0) {
a = w / 11;
space = a / 12;
System.out.println("w: " + w + ", h: " + h + ", a: " + a + "space: " + space);
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 15; y++) {
System.out.println("canvas.drawRect(new Rect(" + (x*a+(x+1)*space) + ", " + (y*a+(y+1)*space) + ", " + a + ", " + a + "), p);");
canvas.drawRect(new Rect(x*a+(x+1)*space, y*a+(y+1)*space, a, a), p);
}
}
}
}
这是输出的第一行:
05-08 15:04:19.511: I/System.out(26349): w: 1080, h: 1701, a: 98space: 8
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 8, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 114, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 220, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 326, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 432, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 538, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 644, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 750, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 856, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 962, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 1068, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 1174, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 1280, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 1386, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(8, 1492, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(114, 8, 98, 98), p);
05-08 15:04:19.511: I/System.out(26349): canvas.drawRect(new Rect(114, 114, 98, 98), p);
这是ui的截图:
那么我做错了什么?应该有一个10x15的矩形矩阵。
答案 0 :(得分:1)
我查看了Rect的文档,他们说这些参数包括left
,top
,right
,bottom
。
从您的日志输出中,top
bottom
(在屏幕上)。它在文档中指出范围检查没有完成,所以我猜测如果一个重叠另一个则不会显示矩形。
希望能帮到你。
答案 1 :(得分:1)
我像这样运行你的代码:
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 15; y++) {
int left = x*a+(x+1)*space;
int top = y*a+(y+1)*space;
int right = a;
int bottom = a;
if( left <= right && top <= bottom ) {
Log.d("draw","canvas.drawRect(new Rect("+left +", "+top+", "+right+", "+bottom+"), p);");
canvas.drawRect(new Rect(left,top,right,bottom), p);
}
}
}
只打印了一个矩形。
canvas.drawRect(new Rect(8, 8, 98, 98), p);
也许它是截图中的矩形。
你想要一个10x15的矩形矩阵。你的意思是这个吗?
protected void onDraw(Canvas canvas) {
Paint p = new Paint();
p.setColor(Color.GRAY);
if (w != 0 && h != 0) {
a = w / 11;
space = a / 12;
System.out.println("w: " + w + ", h: " + h + ", a: " + a + "space: " + space);
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 15; y++) {
int px = x*a+(x+1)*space;
int py = y*a+(y+1)*space;
System.out.println("canvas.drawRect(new Rect("+px+", "+py+", "+(px+a)+", "+(py+a)+"), p);");
canvas.drawRect(new Rect(px, py, px+a, py+a), p);
}
}
}
}