0

Is there a way i can stop the draw method to be called automatically.

What i'm doing currently :

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();
}

I want the view to only redraw when i call the render method, i tried setting willNotDraw(true) and false but it makes no difference. How can i stop the surface view from drawing automatically, it seems to be drawing at average 53-59 fps which is fine but i need the rendering to be done at the end to avoid clashes with the game threads/lists

Android_Dev
  • 41
  • 1
  • 7
  • Please, [review this question](http://stackoverflow.com/questions/3527621/how-to-pause-and-resume-a-surfaceview-thread), to see if it resolves your issue.... As for "Vijayaramanan" answer, NEVER mix threads that should have different scopes. You will get errors/exceptions. If necessary, use Handlers to get the main Looper, and post messages to another Thread, and perfom methods. Finally, if you need a single SurfaceView to start/stop rendering at given times, [override the onDraw()](https://developer.android.com/training/custom-views/custom-drawing.html) of that single `Object`. – Bonatti Aug 29 '16 at 14:31

1 Answers1

-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