绘制surfaceView 基础类

 

public class SurfaceViewTempalte extends SurfaceView implements Callback, Runnable  {
	
	private SurfaceHolder mHolder; 
	private Canvas mCanvas;
	
	// 线程
	Thread t;
	
	// 线程开关
	private boolean isRunning;
	
	
	public SurfaceViewTempalte(Context context) {
		this(context, null);
	}
	public SurfaceViewTempalte(Context context, AttributeSet attrs) {
		super(context, attrs, 0);
		
		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防止back无释放
		try {
			mCanvas = mHolder.lockCanvas();
			if(mCanvas != null) {
				//绘制
				
			}
		} catch (Exception e) {
		}
		finally {
			//释放mCanvas
			if(mCanvas != null) {
				mHolder.unlockCanvasAndPost(mCanvas);
			}
		}
	}

}

  

posted @ 2015-12-02 20:38  说好范特西  阅读(164)  评论(0编辑  收藏  举报