Skip to content

Instantly share code, notes, and snippets.

@febinsathar
Created March 2, 2015 17:38
Show Gist options
  • Save febinsathar/5927bf6f5dea687f42f5 to your computer and use it in GitHub Desktop.
Save febinsathar/5927bf6f5dea687f42f5 to your computer and use it in GitHub Desktop.
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onSizeChanged(width, height, 0, 0);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread = new DrawThread(holder);
drawThread.setRunning(true);
drawThread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
drawThread.setRunning(false);
while (retry) {
try {
drawThread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
class DrawThread extends Thread {
private SurfaceHolder surfaceHolder;
private boolean running = false;
public DrawThread(SurfaceHolder surfaceHolder){
this.surfaceHolder = surfaceHolder;
}
public void setRunning(boolean value){
running = value;
}
@Override
public void run() {
Canvas c;
while (running) {
c = null;
try {
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
draw(c);
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment