Android 如何使画布绘制后功能失效

Android 如何使画布绘制后功能失效,android,surfaceholder,Android,Surfaceholder,我如下调用Draw函数。基本上我想在surface holder中显示弹跳球。 为此,我编写了如下代码: private void getDrawing() { for (i = 0; i < 1000; i++) { if ((i + 1 % 20) == 0) { Rect r = new Rect(0, 0, canvas.getWidth(), canvas.getHeight()); Paint p

我如下调用Draw函数。基本上我想在surface holder中显示弹跳球。 为此,我编写了如下代码:

    private void getDrawing() {

    for (i = 0; i < 1000; i++) {
        if ((i + 1 % 20) == 0) {
            Rect r = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            canvas.drawRect(r, paint);
        }
        canvas = mSurfaceHolder.lockCanvas();

        Draw(canvas);
    }
}


@post(new Runnable() {
       public void run() {
         this.invalidate();
       }

   });

protected void Draw(Canvas c) {



    ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);


    if (x < 0 && y < 0) {
        x = 1000 / 2;
        y = 1000 / 2;
    } else {
        x += xVelocity;
        y += yVelocity;
        if ((x > 1000 - ball.getWidth()) || (x < 0)) {
            xVelocity = xVelocity * -1;
        }
        if ((y > 1000 - ball.getHeight()) || (y < 0)) {
            yVelocity = yVelocity * -1;
        }
    }

    c.drawBitmap(ball, x, y, null);
    mSurfaceHolder.unlockCanvasAndPost(canvas);

}

Problem is the canvas is not getting invalidating and the succesive draw calls are superimposing.How to erase the canvas after each draw as happens in the case of invalidate function.I am also writing the following code but its also not helping :
        Rect r = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
            Paint paint = new Paint();
            paint.setColor(Color.BLACK);
            canvas.drawRect(r, paint);
private void getDrawing(){
对于(i=0;i<1000;i++){
如果((i+1%20)==0){
Rect r=new Rect(0,0,canvas.getWidth(),canvas.getHeight());
油漆=新油漆();
油漆。设置颜色(颜色。黑色);
画布.drawRect(r,油漆);
}
canvas=mSurfaceHolder.lockCanvas();
绘画(画布);
}
}
@post(新的Runnable(){
公开募捐{
这个。使无效();
}
});
受保护的空白绘制(画布c){
ball=BitmapFactory.decodeResource(getResources(),R.drawable.ball);
if(x<0&&y<0){
x=1000/2;
y=1000/2;
}否则{
x+=x速度;
y+=y线性度;
如果((x>1000-ball.getWidth())| |(x<0)){
xVelocity=xVelocity*-1;
}
如果((y>1000-ball.getHeight())| |(y<0)){
yVelocity=yVelocity*-1;
}
}
c、 drawBitmap(球、x、y、空);
mSurfaceHolder.unlockCanvasAndPost(画布);
}
问题是画布没有失效,连续绘制调用正在叠加。如何在每次绘制后擦除画布,就像在invalidate函数中一样。我也在编写以下代码,但也没有帮助:
Rect r=new Rect(0,0,canvas.getWidth(),canvas.getHeight());
油漆=新油漆();
油漆。设置颜色(颜色。黑色);
画布.drawRect(r,油漆);
是否有其他方法可以在曲面保持架上显示反弹球。如果不从View类进行扩展,我们如何在曲面保持架上使用OnDraw和Invalidate函数


在这方面,

您正在后台线程中执行无效操作,而无效操作只能在主线程中运行。如果你想让它持续失效,写下

invalidate();

在onDraw函数的末尾。

持续更新用户界面的线程在哪里?