从自动调用绘图中停止Surface View

时间:2016-08-28 22:49:33

标签: java android

有没有办法可以停止自动调用draw方法。

我目前在做什么:

public class GUI extends extends SurfaceView implements SurfaceHolder.Callback{

...   

public void render() {
    SurfaceHolder surfaceHolder = getHolder();
    Canvas canvas = null;
    try{
        canvas = surfaceHolder.lockCanvas();
        if (canvas != null) {
            draw(canvas);
        }
    }finally {
        if (canvas != null){
            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

@Override
public void draw(Canvas canvas){
... //things done with the canvas object
}
}

public class Game extends AbstractGame {

...
@Override
public void render(){
   this.graphicsPanel.render();
}

我希望视图只在我调用render方法时重绘,我尝试设置willNotDraw(true)和false但它没有区别。如何自动停止绘制表面视图,它似乎平均绘制53-59 fps这很好,但我需要在最后完成渲染以避免与游戏线程/列表发生冲突

1 个答案:

答案 0 :(得分:-1)

Handle the scenario with thread in surface view class

On resume of the activity start the thread of the surface view with 
Thread th;
boolean isthreadStarted=false;
public void OnResume (){
isthreadStarted=true;
th=new Thread (this);
th.start ();
}
public void 0nPause (){
isthreadStarted=true;
if (th!=null){
th.join ();
}
}

IN the render method check for isthreadStarted as true then call the draw method

render (){
if (isthreadStarted){
// call your draw method 
draw (canvas);
}
}

from your activity call this surface view onPause method to stop the canvas draw in this view