我使用SurfaceView绘制了绿色矩形,并通过Button单击事件使其左右移动。第一次单击移动按钮,它可以正常工作。 之后,在第一个位置出现意外的矩形。
这是SurfaceView类中的绘制函数:
public void draw(int x, int y)
{
Canvas canvas = null;
canvas = getHolder().lockCanvas(null);
drawBlock(canvas,x,y);
getHolder().unlockCanvasAndPost(canvas);
}
public void drawBlock(Canvas canvas,int x, int y)
{
squareSize = 50;
if (canvas == null) {
} else {
Paint rectPaint = new Paint();
rectPaint.setColor(Color.GREEN);
canvas.drawRect(new Rect(y * squareSize, x * squareSize, (y + 1) * squareSize, (x + 1) * squareSize),rectPaint);
}
X = x;
Y = y;
}
这是向左移动onclick事件:
btnLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int x = customSurfaceView.getx();
int y = customSurfaceView.gety();
customSurfaceView.draw(x, y - 1);
}
});
请帮助解决此问题!