首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在画布上穿过“单元格”的Daw线

在画布上穿过“单元格”的Daw线
EN

Stack Overflow用户
提问于 2018-06-05 16:46:21
回答 1查看 46关注 0票数 0

我需要弄清楚如何画垂直和水平线通过一个细胞的中心。

顺便说一句,我有一个有100x100个细胞的2D网格,如何在这些细胞内部画出一条线,将每个细胞分成4部分?

我用咆哮画:

代码语言:javascript
复制
//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)

找不到一种方法来画其他的线,这一次通过在每个细胞内,使它分裂成四个部分。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-05 16:52:43

代码语言:javascript
复制
//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);
    }
}

作为附带说明,我会考虑使用函数而不是重复代码!祝你好运;)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50705175

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档