Created
April 8, 2011 21:20
-
-
Save aconbere/910760 to your computer and use it in GitHub Desktop.
wootzor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.conbere.anders.escape; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Paint; | |
import android.graphics.RectF; | |
import android.util.AttributeSet; | |
import android.view.SurfaceView; | |
import android.view.SurfaceHolder; | |
public class EscapeView extends SurfaceView implements SurfaceHolder.Callback { | |
class EscapeThread extends Thread { | |
public SurfaceHolder surfaceHolder; | |
public Context context; | |
private boolean running; | |
public EscapeThread(SurfaceHolder holder, Context cont) { | |
this.surfaceHolder = holder; | |
this.context = cont; | |
this.running = false; | |
} | |
public void setRunning(boolean b) { | |
this.running = b; | |
} | |
private void doDraw(Canvas canvas) { | |
// Paint paint = new Paint(); | |
// paint.setAntiAlias(true); | |
// paint.setARGB(255, 0, 255, 0); | |
// | |
// RectF rect = new RectF(100, 100, 100, 100); | |
// | |
// canvas.drawRect(rect, paint); | |
} | |
@Override | |
public void run() { | |
while (running) { | |
Canvas c = null; | |
try { | |
c = surfaceHolder.lockCanvas(null); | |
synchronized (surfaceHolder) { | |
doDraw(c); | |
} | |
} finally { | |
if (c != null) { | |
surfaceHolder.unlockCanvasAndPost(c); | |
} | |
} | |
} | |
} | |
} | |
private Thread thread; | |
public Context context; | |
public EscapeView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
this.context = context; | |
SurfaceHolder holder = getHolder(); | |
holder.addCallback(this); | |
this.thread = new EscapeThread(holder, context); | |
} | |
@Override | |
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
} | |
@Override | |
public void surfaceCreated(SurfaceHolder holder) { | |
this.thread.setRunning(true); | |
this.thread.run(); | |
} | |
@Override | |
public void surfaceDestroyed(SurfaceHolder holder) { | |
// we have to tell thread to shut down & wait for it to finish, or else | |
// it might touch the Surface after we return and explode | |
boolean retry = true; | |
this.thread.setRunning(false); | |
while (retry) { | |
try { | |
this.thread.join(); | |
retry = false; | |
} catch (InterruptedException e) { | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment