Skip to content

Instantly share code, notes, and snippets.

@jingwings
Created April 5, 2016 08:16
Show Gist options
  • Save jingwings/6c4015da25c32a33e4b130944b3a78a2 to your computer and use it in GitHub Desktop.
Save jingwings/6c4015da25c32a33e4b130944b3a78a2 to your computer and use it in GitHub Desktop.
SurfaceViewTempalte
public class SurfaceViewTempalte extends SurfaceView implements SurfaceHolder.Callback, Runnable {
private SurfaceHolder mHolder;
private Canvas mCanvas;
private Thread t;
private boolean isRunning;
public SurfaceViewTempalte(Context context) {
super(context, null);
}
public SurfaceViewTempalte(Context context, AttributeSet attrs) {
super(context, attrs);
mHolder = getHolder();
mHolder.addCallback(this);
//可获得焦点
setFocusable(true);
setFocusableInTouchMode(true);
//设置常量
setKeepScreenOn(true);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
isRunning = true;
t = new Thread(this);
t.start();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
isRunning = false;
}
@Override
public void run() {
while (isRunning) {
draw();
}
}
private void draw() {
try {
mCanvas = mHolder.lockCanvas();
if (mCanvas != null) {
}
} catch (Exception e) {
} finally {
if (mCanvas != null) {
mHolder.unlockCanvasAndPost(mCanvas);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment