Android SurfaceView学习示例

2019-02-19 18:02:16 浏览数 (1)

SurfaceView是View的子类,使用的方式与任何View所派生的类都是完全相同的,可以像其他View那样应用动画,并把它们放到布局中。  SurfaceView封装的Surface支持使用本章前面所描述的所有标准Canvas方法进行绘图,同时也支持完全的OpenGL ES库。  使用OpenGL,你可以再Surface上绘制任何支持的2D或者3D对象,与在2D画布上模拟相同的效果相比,这种方法可以依靠硬件加速(可用的时候)来极大地提高性能。  对于显示动态的3D图像来说,例如,那些使用Google Earth功能的应用程序,或者那些提供沉浸体验的交互式游戏,SurfaceView特别有用。它还是实时显示摄像头预览的最佳选择。 

SurfaceView 和 View 的明显不同之处在于:

1、继承SurfaceView  的视图可以另起一个线程,或者说在子线程中更新视图。

2、 SurfaceView 的画图方法是在子线程中执行的 而 View类的那个示例 的画图方法是在UI线程中执行的。

3、SurfaceView在绘图之前必须使用lockCanvas 方法锁定画布,并得到画布,然后再画布上绘制;当绘制完成后,使用unlockCanvasAndPost 方法解锁画布,然后就显示到屏幕上。

SurfaceView 类的事件处理规则和View一样。

具体示例:

Activity

代码语言:javascript复制
public class Activity01 extends Activity {
	GameSurfaceView mGameSurfaceView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mGameSurfaceView = new GameSurfaceView(this);
		setContentView(mGameSurfaceView);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			mGameSurfaceView.x = event.getX();
			mGameSurfaceView.y = event.getY();
		}
		
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			this.finish();
		}
		
		return true;
	}
}

GameSurfaceView

代码语言:javascript复制
public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

	boolean mbLoop = false;
	SurfaceHolder mSurfaceHolder = null;
	
	int count = 0;
	float x = 50, y = 50;
	int screenWidth = 480, screenHeight = 800;

	public GameSurfaceView(Context context) {
		super(context);

		mbLoop = true;
		
		mSurfaceHolder = this.getHolder();
		mSurfaceHolder.addCallback(this);
		this.setFocusable(true);
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();	// start paint thread
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		screenWidth = width;		// reset width when screen orientation is changed
		screenHeight = height;		// reset height when screen orientation is changed
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mbLoop = false;
	}

	@Override
	public void run() {
		while (mbLoop) {
			synchronized (mSurfaceHolder) {
				onDraw();
			}

			try {
				Thread.sleep(200);
			} catch (Exception e) {
			}
		}
	}

	public void onDraw() {
		Canvas canvas = mSurfaceHolder.lockCanvas();
		if (mSurfaceHolder == null || canvas == null) {
			return;
		}
		
		if (count < 100) {
			count  ;
		} else {
			count = 0;
		}
		
		Paint mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.CYAN);
		canvas.drawRect(0, 0, screenWidth, screenHeight, mPaint);	// repaint background color
		switch (count % 4) {
		case 0:
			mPaint.setColor(Color.BLUE);
			break;
		case 1:
			mPaint.setColor(Color.GREEN);
			break;
		case 2:
			mPaint.setColor(Color.RED);
			break;
		case 3:
			mPaint.setColor(Color.YELLOW);
			break;
		default:
			mPaint.setColor(Color.WHITE);
			break;
		}
		canvas.drawCircle(x, y, 50, mPaint);
		mSurfaceHolder.unlockCanvasAndPost(canvas);
	}
}

运行效果:

源码下载

0 人点赞