Android游戲開發(fā)之七:自定義SurfaceView
作者:佚名
本文簡單討論以后Android游戲引擎模板的架構(gòu)問題。在Android游戲開發(fā)教程之二:View類與SurfaceView類中我們已經(jīng)談到,SurfaceView類是有很多優(yōu)勢的,所以在Android游戲開發(fā)中還是選擇SurfaceView。
這里我們直接繼承SurfaceView,實現(xiàn)SurfaceHolder.Callback接口,處理surfaceCreated、 surfaceChanged以及surfaceDestroyed方法,這里我們并沒有把按鍵控制傳入,最終游戲的控制方面仍然由View內(nèi)部類處理比 較好,有關(guān)SurfaceView的具體我們可以參見Android開源項目的Camera中有關(guān)畫面捕捉以及VideoView的控件實現(xiàn)大家可以清晰 了解最終的用意。
- public class cwjView extends SurfaceView implements SurfaceHolder.Callback {
- public cwjView(Context context, AttributeSet attrs) {
- super(context, attrs);
- SurfaceHolder holder=getHolder();
- holder.addCallback(this);
- setFocusable(true);
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
- }
- public void surfaceCreated(SurfaceHolder holder) {
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- }
- @Override
- public void onWindowFocusChanged(boolean hasWindowFocus) {
- }
- }
責(zé)任編輯:閆佳明
來源:
jizhuomi