【问题标题】:Change canvas the color of cell onTouch down更改画布上单元格的颜色 onTouch down
【发布时间】:2019-03-15 14:01:58
【问题描述】:

我实际上是在基于网格的 Android Studio 上做一个游戏,我用这个来绘制:

public void dessin(Canvas canvas){
    paint.reset();
    TenGame theGame=gam.getTenGame();
    canvas.drawColor(Color.YELLOW);

    paint.setColor(Color.BLACK);
    for(int x=0;x<canvasWidth;x+=cellSize){
        canvas.drawLine(x,0,x,canvasWidth,paint);
    }
    for(int y=0;y<canvasWidth;y+=cellSize){
        canvas.drawLine(0,y,canvasWidth,y,paint);
    }

    paint.setTextSize(50);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    PositionList plist=theGame.getSelectedGroup();

    for(int x=0;x<5;x++){
        for(int y=0;y<5;y++){
            Position p=new Position(x,y);
            if (theGame.get(p)!=null) {
                canvas.drawText(Integer.toString(theGame.get(p)), (x * cellSize) + 15, (cellSize + y * cellSize) - 15, paint);
            }
            else{
                canvas.drawText("", (x * cellSize) + 15, (cellSize + y * cellSize) - 15, paint);
            }
        }
    }
}

我明白了:
格子游戏:

当我触摸网格中的一个单元格时,我想更改背景颜色。
我认为对我来说最好的解决方案是做一个drawRect(),但我不知道我应该做哪个维度。
我在库中有游戏模型,可以得到Position (x, y)。

【问题讨论】:

    标签: java android canvas drawing


    【解决方案1】:

    使用 GridPane 可能会更好,因为这将使绘制光栅线和向每个单元格添加事件处理程序的过程更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-14
      • 1970-01-01
      • 2014-08-24
      • 2015-01-24
      • 1970-01-01
      • 2019-01-05
      • 2013-03-09
      相关资源
      最近更新 更多