Android SurfaceHolder処理SurfaceViewの図面
Surface Viewによるゲーム開発では、Canvasに描かれた効果やアニメーションをSurface Holderで処理する必要があります.表面、サイズ、ピクセルなどを制御します.Abstract interface to someone holding a display surface. Allows you to control the surface size and format, edit the pixels in the surface, and monitor changes to the surface. This interface is typically available through the SurfaceView class. 特に、abstract void addCallback(SurfaceHolder.Callback callback);//Surface Viewの現在の所有者にコールバックオブジェクトを与えます.abstract Canvas lockCanvas();//キャンバスをロックすると、一般的にはロック後に戻るキャンバスオブジェクトCanvasを介して、その上に絵を描くなどの操作ができます.abstract Canvas lockCanvas(Rect dirty);//キャンバスの領域をロックして絵を描くなど..図を描くと、次のunlockCanvasAndPostを呼び出して表示内容を変更します.//部分メモリの要求が比較的高いゲームに対してdirty外の他の領域の画素を再描画することなく、速度を向上させることができる.abstract void unlockCanvasAndPost(Canvas canvas);//図面のロックを終了し、変更をコミットします.例:
// :AndroidでSurface Viewでゲーム
class DrawThread extends Thread {
private SurfaceHolder holder;
private boolean running = true;
protected DrawThread(SurfaceHolder holder) {this.holder = holder;}
protected void doStop() { running = false; }
public void run() {
Canvas c = null;
while( running ) {
c = holder.lockCanvas(null);
// , , null
try {
synchronized(holder) {
bGrid.drawGrid(c);//
BBoom.drawBooms(c, booms); //
bFairy.drawFairy(c);//
// z , 。
}
} catch(Exception ex) {}
finally {
holder.unlockCanvasAndPost(c);
//
}
}
}
};
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) {}
}