Android——在SurfaceView上绘图

1、自定义View继承SurfaceView并实现SurfaceHolder.Callback接口;

2、声明一个SurfaceHolder对象用于控制SurfaceView;

3、在构造函数中获取holder并添加回调:

public DrawView(Context context) {
	super(context);
	holder = this.getHolder();
	holder.addCallback(this);
}

4、使用Runnable定义一个绘图线程:

class drawThread implements Runnable {
	public void run() {
		while (true) {
			Canvas canvas = holder.lockCanvas();  // 获取画布
			startDraw(canvas);
			holder.unlockCanvasAndPost(canvas);  // 解锁画布,提交图像
		}
	}
}

5、在surfaceCreated方法中创建该绘图线程:

public void surfaceCreated(SurfaceHolder holder) {
	new Thread(new drawThread()).start();
}

你可能感兴趣的:(android,绘图,SurfaceView)