Canvas.drawRect 在 for-each 循环中只工作一次
Canvas.drawRect works only once in a for-each loop
我正在尝试为我的设备创建一个自定义键盘,我想为键盘中的每个键绘制我自己的自定义背景。出于某种原因,canvas 只会绘制第一个键的背景,而忽略其余部分。
这是我的 onDraw 函数的来源:
@Override
public void onDraw(Canvas canvas) {
Paint paintBack = new Paint();
paintBack.setStyle(Paint.Style.FILL);
paintBack.setColor(Color.DKGRAY);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
Rect fillRect = new Rect();
fillRect.left = key.x + keyMargin;
fillRect.right = key.width - keyMargin;
fillRect.top = key.y + keyMargin;
fillRect.bottom = key.height - keyMargin;
System.out.println("X: " + Integer.toString(key.x) + ", Y: " + Integer.toString(key.y) + ", W: " + Integer.toString(key.width) + ", H: " + Integer.toString(key.height));
canvas.drawRect(fillRect, paintBack);
}
}
这是我为每个键发出的 System.out.println
调用收到的输出:
X: 0, Y: 0, W: 79, H: 80
X: 79, Y: 0, W: 79, H: 80
X: 158, Y: 0, W: 79, H: 80
X: 237, Y: 0, W: 79, H: 80
X: 316, Y: 0, W: 79, H: 80
X: 395, Y: 0, W: 79, H: 80
X: 474, Y: 0, W: 79, H: 80
X: 553, Y: 0, W: 79, H: 80
X: 632, Y: 0, W: 79, H: 80
X: 711, Y: 0, W: 79, H: 80
X: 790, Y: 0, W: 79, H: 80
X: 869, Y: 0, W: 79, H: 80
X: 948, Y: 0, W: 79, H: 80
X: 1027, Y: 0, W: 158, H: 80
X: 0, Y: 80, W: 119, H: 80
X: 119, Y: 80, W: 79, H: 80
X: 198, Y: 80, W: 79, H: 80
X: 277, Y: 80, W: 79, H: 80
X: 356, Y: 80, W: 79, H: 80
X: 435, Y: 80, W: 79, H: 80
X: 514, Y: 80, W: 79, H: 80
X: 593, Y: 80, W: 79, H: 80
X: 672, Y: 80, W: 79, H: 80
X: 751, Y: 80, W: 79, H: 80
X: 830, Y: 80, W: 79, H: 80
X: 909, Y: 80, W: 79, H: 80
X: 988, Y: 80, W: 79, H: 80
X: 1067, Y: 80, W: 119, H: 80
X: 0, Y: 160, W: 139, H: 80
X: 139, Y: 160, W: 79, H: 80
X: 218, Y: 160, W: 79, H: 80
X: 297, Y: 160, W: 79, H: 80
X: 376, Y: 160, W: 79, H: 80
X: 455, Y: 160, W: 79, H: 80
X: 534, Y: 160, W: 79, H: 80
X: 613, Y: 160, W: 79, H: 80
X: 692, Y: 160, W: 79, H: 80
X: 771, Y: 160, W: 79, H: 80
X: 850, Y: 160, W: 79, H: 80
X: 929, Y: 160, W: 79, H: 80
X: 1008, Y: 160, W: 178, H: 80
X: 0, Y: 240, W: 178, H: 80
X: 178, Y: 240, W: 79, H: 80
X: 257, Y: 240, W: 79, H: 80
X: 336, Y: 240, W: 79, H: 80
X: 415, Y: 240, W: 79, H: 80
X: 494, Y: 240, W: 79, H: 80
X: 573, Y: 240, W: 79, H: 80
X: 652, Y: 240, W: 79, H: 80
X: 731, Y: 240, W: 79, H: 80
X: 810, Y: 240, W: 79, H: 80
X: 889, Y: 240, W: 79, H: 80
X: 968, Y: 240, W: 218, H: 80
X: 0, Y: 320, W: 119, H: 80
X: 119, Y: 320, W: 99, H: 80
X: 218, Y: 320, W: 717, H: 80
X: 935, Y: 320, W: 99, H: 80
X: 1034, Y: 320, W: 158, H: 80
输出清楚地显示了所有键的所有坐标和大小,但由于某些原因,canvas.drawRect
方法只会触发一次。
我在这里忽略了什么,还是我只是以错误的方式接近它?
根据@pskink 的评论,我将代码更改为如下所示:
@Override
public void onDraw(Canvas canvas) {
Paint paintBack = new Paint();
paintBack.setStyle(Paint.Style.FILL);
paintBack.setColor(Color.DKGRAY);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
Rect fillRect = new Rect();
fillRect.left = key.x + keyMargin;
fillRect.right = key.x + key.width - keyMargin;
fillRect.top = key.y + keyMargin;
fillRect.bottom = key.y + key.height - keyMargin;
System.out.println("X: " + Integer.toString(key.x) + ", Y: " + Integer.toString(key.y) + ", W: " + Integer.toString(key.width) + ", H: " + Integer.toString(key.height));
canvas.drawRect(fillRect, paintBack);
}
}
(请注意,我将 fillRect.right
更改为 key.x + key.width - keyMargin
,将 fillRect.bottom
更改为 key.y + key.height - keyMargin
)
这对我有用。
我正在尝试为我的设备创建一个自定义键盘,我想为键盘中的每个键绘制我自己的自定义背景。出于某种原因,canvas 只会绘制第一个键的背景,而忽略其余部分。
这是我的 onDraw 函数的来源:
@Override
public void onDraw(Canvas canvas) {
Paint paintBack = new Paint();
paintBack.setStyle(Paint.Style.FILL);
paintBack.setColor(Color.DKGRAY);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
Rect fillRect = new Rect();
fillRect.left = key.x + keyMargin;
fillRect.right = key.width - keyMargin;
fillRect.top = key.y + keyMargin;
fillRect.bottom = key.height - keyMargin;
System.out.println("X: " + Integer.toString(key.x) + ", Y: " + Integer.toString(key.y) + ", W: " + Integer.toString(key.width) + ", H: " + Integer.toString(key.height));
canvas.drawRect(fillRect, paintBack);
}
}
这是我为每个键发出的 System.out.println
调用收到的输出:
X: 0, Y: 0, W: 79, H: 80
X: 79, Y: 0, W: 79, H: 80
X: 158, Y: 0, W: 79, H: 80
X: 237, Y: 0, W: 79, H: 80
X: 316, Y: 0, W: 79, H: 80
X: 395, Y: 0, W: 79, H: 80
X: 474, Y: 0, W: 79, H: 80
X: 553, Y: 0, W: 79, H: 80
X: 632, Y: 0, W: 79, H: 80
X: 711, Y: 0, W: 79, H: 80
X: 790, Y: 0, W: 79, H: 80
X: 869, Y: 0, W: 79, H: 80
X: 948, Y: 0, W: 79, H: 80
X: 1027, Y: 0, W: 158, H: 80
X: 0, Y: 80, W: 119, H: 80
X: 119, Y: 80, W: 79, H: 80
X: 198, Y: 80, W: 79, H: 80
X: 277, Y: 80, W: 79, H: 80
X: 356, Y: 80, W: 79, H: 80
X: 435, Y: 80, W: 79, H: 80
X: 514, Y: 80, W: 79, H: 80
X: 593, Y: 80, W: 79, H: 80
X: 672, Y: 80, W: 79, H: 80
X: 751, Y: 80, W: 79, H: 80
X: 830, Y: 80, W: 79, H: 80
X: 909, Y: 80, W: 79, H: 80
X: 988, Y: 80, W: 79, H: 80
X: 1067, Y: 80, W: 119, H: 80
X: 0, Y: 160, W: 139, H: 80
X: 139, Y: 160, W: 79, H: 80
X: 218, Y: 160, W: 79, H: 80
X: 297, Y: 160, W: 79, H: 80
X: 376, Y: 160, W: 79, H: 80
X: 455, Y: 160, W: 79, H: 80
X: 534, Y: 160, W: 79, H: 80
X: 613, Y: 160, W: 79, H: 80
X: 692, Y: 160, W: 79, H: 80
X: 771, Y: 160, W: 79, H: 80
X: 850, Y: 160, W: 79, H: 80
X: 929, Y: 160, W: 79, H: 80
X: 1008, Y: 160, W: 178, H: 80
X: 0, Y: 240, W: 178, H: 80
X: 178, Y: 240, W: 79, H: 80
X: 257, Y: 240, W: 79, H: 80
X: 336, Y: 240, W: 79, H: 80
X: 415, Y: 240, W: 79, H: 80
X: 494, Y: 240, W: 79, H: 80
X: 573, Y: 240, W: 79, H: 80
X: 652, Y: 240, W: 79, H: 80
X: 731, Y: 240, W: 79, H: 80
X: 810, Y: 240, W: 79, H: 80
X: 889, Y: 240, W: 79, H: 80
X: 968, Y: 240, W: 218, H: 80
X: 0, Y: 320, W: 119, H: 80
X: 119, Y: 320, W: 99, H: 80
X: 218, Y: 320, W: 717, H: 80
X: 935, Y: 320, W: 99, H: 80
X: 1034, Y: 320, W: 158, H: 80
输出清楚地显示了所有键的所有坐标和大小,但由于某些原因,canvas.drawRect
方法只会触发一次。
我在这里忽略了什么,还是我只是以错误的方式接近它?
根据@pskink 的评论,我将代码更改为如下所示:
@Override
public void onDraw(Canvas canvas) {
Paint paintBack = new Paint();
paintBack.setStyle(Paint.Style.FILL);
paintBack.setColor(Color.DKGRAY);
List<Key> keys = getKeyboard().getKeys();
for(Key key: keys) {
Rect fillRect = new Rect();
fillRect.left = key.x + keyMargin;
fillRect.right = key.x + key.width - keyMargin;
fillRect.top = key.y + keyMargin;
fillRect.bottom = key.y + key.height - keyMargin;
System.out.println("X: " + Integer.toString(key.x) + ", Y: " + Integer.toString(key.y) + ", W: " + Integer.toString(key.width) + ", H: " + Integer.toString(key.height));
canvas.drawRect(fillRect, paintBack);
}
}
(请注意,我将 fillRect.right
更改为 key.x + key.width - keyMargin
,将 fillRect.bottom
更改为 key.y + key.height - keyMargin
)
这对我有用。