我有一个扫雷应用程序正在运行,一切基本正常,直到我想重置板,以便我可以玩一个新的游戏。我有一个单独的方法调用drawboard(),我用它来绘制板
我的问题是,我想重新绘制电路板在其初始状态与所有的方块回到他们原来的颜色,但我不知道一个好的方法来实现这一点后,点击我的重新启动按钮。画布位于自定义视图类中
在我的主活动中尝试在onclicklistener中调用drawboard()是行不通的,我可以理解为什么,但是在自定义视图类的任何地方尝试使用onclicklistener都会引发异常
我环顾四周,似乎另一种选择是在我的线性布局中的现有按钮上画一个不可见的按钮,这是唯一有效的方法还是有方法检查按钮是否被单击然后调用drawboard()?
更新:包括drawboard()和clear()的原始代码
@重写受保护的void ondraw(canvas){super.ondraw(canvas);
drawBoard(canvas);
int sqWidth= getWidth()/ columns;
int sqlength = getHeight()/rows;
rect = new Rect( 0, 0, sqWidth, sqlength);
//from the list of saved points we can utilize this to paint our squared the was we want it
for (Point p: pointsList){
if (Model.getInstance().getFieldContent(p.x, p.y) == Model.getInstance().mine){
canvas.save();
canvas.translate(p.x * sqWidth, p.y * sqlength);
canvas.drawRect(rect, mine);
canvas.drawBitmap(bomb, p.x, p.y, bombpaint);
canvas.restore();
}
else {
canvas.save();
canvas.translate(p.x * sqWidth, p.y * sqlength);
canvas.drawRect(rect, square);
canvas.restore();
}
}
}
public void drawBoard(Canvas canvas) {
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
canvas.drawLine(0, getHeight() / rows, getWidth(), getHeight() / rows, Lines);
canvas.drawLine(0, 2 * getHeight() / rows, getWidth(), 2 * getHeight() / rows, Lines);
canvas.drawLine(0, 3 * getHeight() / rows, getWidth(), 3 * getHeight() / rows, Lines);
canvas.drawLine(0, 4 * getHeight() / rows, getWidth(), 4 * getHeight() / rows, Lines);
canvas.drawLine(0, 5 * getHeight() / rows, getWidth(), 5 * getHeight() / rows, Lines);
canvas.drawLine(0, 6 * getHeight() / rows, getWidth(), 6 * getHeight() / rows, Lines);
canvas.drawLine(0, 7 * getHeight() / rows, getWidth(), 7 * getHeight() / rows, Lines);
canvas.drawLine(0, 8 * getHeight() / rows, getWidth(), 8 * getHeight() / rows, Lines);
canvas.drawLine(0, 9 * getHeight() / rows, getWidth(), 9 * getHeight() / rows, Lines);
canvas.drawLine(0, 10 * getHeight() / rows, getWidth(), 10 * getHeight() / rows, Lines);
// 9 vertical lines
canvas.drawLine(getWidth() / columns, 0, getWidth() / columns, getHeight(), Lines);
canvas.drawLine(2 * getWidth() / columns, 0, 2 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(3 * getWidth() / columns, 0, 3 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(4 * getWidth() / columns, 0, 4 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(5 * getWidth() / columns, 0, 5 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(6 * getWidth() / columns, 0, 6 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(7 * getWidth() / columns, 0, 7 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(8 * getWidth() / columns, 0, 8 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(9 * getWidth() / columns, 0, 9 * getWidth() / columns, getHeight(), Lines);
canvas.drawLine(10 * getWidth() / columns, 0, 10 * getWidth() / columns, getHeight(), Lines); //With this the squares should be drawn
}
public void clear ()
{
path = new Path();
postInvalidate();
}
暂无答案!
目前还没有任何答案,快来回答吧!