在android中開發遊戲,一般來說,或想寫一個復雜一點的遊戲,是必須用到SurfaceView來開發的。
經過這一陣子對android的學習,我找到瞭自已在android中遊戲開發的誤區,不要老想著用Layout和view去實現,不要將某個遊戲中的對象做成一個組件來處理。應該盡量想著在Canvas(畫佈)中畫出遊戲戲中的背景、人物、動畫等…
SurfaceView提供直接訪問一個可畫圖的界面,可以控制在界面頂部的子視圖層。SurfaceView是提供給需要直接畫像素而不是使用窗體部件的應用使用的。Android圖形系統中一個重要的概念和線索是surface。View及其子類(如TextView, Button)要畫在surface上。每個surface創建一個Canvas對象(但屬性時常改變),用來管理view在surface上的繪圖操作,如畫點畫線。
還要註意的是,使用它的時候,一般都是出現在最頂層的:The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it.
使用的SurfaceView的時候,一般情況下還要對其進行創建,銷毀,改變時的情況進行監視,這就要用到SurfaceHolder.Callback.class BBatt extends SurfaceView implements SurfaceHolder.Callback {
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){}
//看其名知其義,在surface的大小發生改變時激發
public void surfaceCreated(SurfaceHolder holder){}
//同上,在創建時激發,一般在這裡調用畫圖的線程。
public void surfaceDestroyed(SurfaceHolder holder) {}
//同上,銷毀時激發,一般在這裡將畫圖的線程停止、釋放。
}
例子:
public class BBatt extends SurfaceView implements SurfaceHolder.Callback, OnKeyListener {
private BFairy bFairy;
private DrawThread drawThread;
public BBatt(Context context) {
super(context);
this.setLayoutParams(new ViewGroup.LayoutParams(Global.battlefieldWidth,Global.battlefieldHeight));
this.getHolder().addCallback( this );
this.setFocusable( true );
this.setOnKeyListener( this );
bFairy = new BFairy(this.getContext());
}
public void surfaceChanged(SurfaceHolder holder, int format,int width,int height) {
drawThread = new DrawThread(holder);
drawThread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
if( drawThread != null ) {
drawThread.doStop();
while (true) try {
drawThread.join();
break ;
} catch(Exception ex) {}
}
}
public boolean onKey(View view, int keyCode, KeyEvent event) {}
}
實例2:用線程畫一個藍色的長方形。
package com.g3.test;
/*
* SurfaceView的示例程序
* 演示其流程
*/
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class Test extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
//內部類
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);
holder.unlockCanvasAndPost(canvas);//解鎖畫佈,提交畫好的圖像
}
}
}
}
訪問SurfaceView的底層圖形是通過SurfaceHolder接口來實現的,通過getHolder()方法可以得到這個SurfaceHolder對象。你應該實現surfaceCreated(SurfaceHolder)和surfaceDestroyed(SurfaceHolder)方法來知道在這個Surface在窗口的顯示和隱藏過程中是什麼時候創建和銷毀的。
SurfaceView可以在多線程中被訪問。
註意:一個SurfaceView隻在SurfaceHolder.Callback.surfaceCreated() 和 SurfaceHolder.Callback.surfaceDestroyed()調用之間是可用的,其他時間是得不到它的Canvas對象的(null)。
我的訪問過程:
創建一個SurfaceView的子類,實現SurfaceHolder.Callback接口。
得到這個SurfaceView的SurfaceHolder對象holder。
holder.addCallback(callback),也就是實現SurfaceHolder.Callback接口的類對象。
在SurfaceHolder.Callback.surfaceCreated()調用過後holder.lockCanvas()對象就可以得到SurfaceView對象對應的Canvas對象canvas瞭。
用canvas對象畫圖。
畫圖結束後調用holder.unlockCanvasAndPost()就把圖畫在窗口中瞭。
SurfaceView可以多線程訪問,在多線程中畫圖。
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class MySurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private Context mContext;
private SurfaceHolder mHolder;
public TouchScreenAdjusterSurfaceView(Context context,) {
super(context);
mContext = context;
mHolder = TouchScreenAdjusterSurfaceView.this.getHolder();
mHolder.addCallback(TouchScreenAdjusterSurfaceView.this);
this.setFocusableInTouchMode(true); // to make sure that we can get
// touch events and key events,and
// "setFocusable()" to make sure we
// can get key events
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//now you can get the Canvas and draw something here
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void drawMyShape(PointPostion ps) {
mCanvas = mHolder.lockCanvas();
// draw anything you like
mHolder.unlockCanvasAndPost(mCanvas);
}
}
作者“惡人谷”