package com.qiaoguo.surfaceviewproject;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/**
* Created by Administrator on 2018/3/20.
*/
public class SurfaceViewTemplate extends SurfaceView implements Runnable {
private Thread mThread;
private volatile boolean isRunning;
public SurfaceViewTemplate(Context context, AttributeSet attrs) {
super(context, attrs);
SurfaceHolder holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mThread = new Thread(SurfaceViewTemplate.this);
mThread.start();
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
isRunning=false;
}
});
}
@Override
public void run() {
while (isRunning) {
drawSelf();
}
}
private void drawSelf() {
Canvas canvas=null;
try {
getHolder().lockCanvas();
if (canvas!=null){
//绘制
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (canvas!=null){
getHolder().unlockCanvasAndPost(canvas);
}
}
}
}
网友评论