在 canvas 上绘制穿过 "cells" 的线
Daw lines passing throught "cells" on canvas
我需要弄清楚如何绘制经过单元格中心的垂直线和水平线。
顺便说一句,我有一个包含 100x100 个单元格的二维网格,我如何绘制穿过这些单元格的线,将每个单元格分成 4 部分?
我用波纹管画:
//Draw grid lines horizontally and vertically
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
这画了一些与此类似的东西(每个单元格都有一个 GestureDetector)
无法找到一种方法来在每个单元格内绘制这次经过的其他线条,以便将其分为四个部分。
像这样(红色是细胞):
//Draw grid lines horizontally and vertically
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
xOffset += cellWidth * 0.5f;
yOffset += cellHeight * 0.5f;
//Draw grid lines horizontally and vertically AGAIN.. but now with offsets moved half size to the right/bottom
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
附带说明一下,我会考虑使用函数而不是重复代码!祝你好运 ;)
我需要弄清楚如何绘制经过单元格中心的垂直线和水平线。
顺便说一句,我有一个包含 100x100 个单元格的二维网格,我如何绘制穿过这些单元格的线,将每个单元格分成 4 部分?
我用波纹管画:
//Draw grid lines horizontally and vertically
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
这画了一些与此类似的东西(每个单元格都有一个 GestureDetector)
无法找到一种方法来在每个单元格内绘制这次经过的其他线条,以便将其分为四个部分。
像这样(红色是细胞):
//Draw grid lines horizontally and vertically
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
xOffset += cellWidth * 0.5f;
yOffset += cellHeight * 0.5f;
//Draw grid lines horizontally and vertically AGAIN.. but now with offsets moved half size to the right/bottom
for (int i = 0; i < gameBoard.length; i++) {
if((i * cellWidth) + xOffset > 0 && (i * cellWidth) + xOffset < width) {
canvas.drawLine((i * cellWidth) + xOffset, 0, (i * cellWidth) + xOffset, height, blackPaint);
}
}
for (int i = 0; i < gameBoard[0].length; i++) {
if((i * cellHeight) + yOffset > 0 && (i * cellHeight) + yOffset < height) {
canvas.drawLine(0, (i * cellHeight) + yOffset, width, (i * cellHeight) + yOffset, blackPaint);
}
}
附带说明一下,我会考虑使用函数而不是重复代码!祝你好运 ;)