代码之家  ›  专栏  ›  技术社区  ›  esQmo_

daw线穿过画布上的“单元”

  •  0
  • esQmo_  · 技术社区  · 6 年前

    我需要弄清楚如何画垂直线和水平线穿过单元格的中心。

    顺便说一下,我有一个有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);
            }
        }
    

    这与此类似(每个细胞都有一个手势检测器)

    enter image description here

    找不到方法在每个单元格中绘制通过此时间的其他行,以便将其分成四部分。

    像这样的东西(红色是细胞):

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   abpdev    6 年前
    //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);
        }
    }
    

    另外,我会考虑使用函数而不是重复代码!祝你好运!)