SurfaceView的最基础代码段

1.Activity中引用:
 
setContentView(new MyView(this));


2.MyView的类构建:
class MyView extends SurfaceView implements SurfaceHolder.Callback {

		SurfaceHolder holder;

		public MyView(Context context) {
			super(context);
			holder = this.getHolder();// 获取holder
			holder.addCallback(this);
			// setFocusable(true);

		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {

		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			new Thread(new MyThread()).start();
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {

		}

		// 内部类的内部类
		class MyThread implements Runnable {

			@Override
			public void run() {
				Canvas canvas = holder.lockCanvas(null);// 获取画布
				Paint mPaint = new Paint();
				mPaint.setColor(Color.BLUE);
				canvas.drawRect(new RectF(40, 60, 80, 80), mPaint);
				canvas.drawText("hello", 100, 30, mPaint);
				holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像
			}
		}
	}

你可能感兴趣的:(thread)