當我們需要開發一個複雜遊戲的時候,而且對程式的執行效率要求很高時,View類就不能滿足需求了,這時必須用SurfaceView類進行開發。
例如,對速度要求很高的遊戲時,View類就不能滿足需求了,這時必須使用SurfaceView類進行開發。例如,對速度要求很高的遊戲,可以使用雙緩衝來顯示。遊戲中的背景、人物、動畫等都需要繪製在一個畫布(Canvas)上,而SurfaceView可以直接存取一個畫布,SurfaceView 是提供給需要直接畫像素而不是使用表單組件的應用使用的。 每個Surface建立一個Canvas對象(但屬性時常改變),用來管理View和Surface上的繪圖操作。
具體樣本:
Activity
public class Activity01 extends Activity {</p><p>GameSurfaceView gameView = null;</p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);</p><p> gameView = new GameSurfaceView(this);<br /> setContentView(gameView);<br /> }<br />}
GameSurfaceView
public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable{</p><p>private Activity01 mActivity = null;</p><p>private SurfaceHolder mHolder = null;<br />private Thread mThread = null;;<br />private boolean mLoop = false;</p><p>private Canvas mCanvas = null;<br />private Paint mPaint = null;<br />private long drawStartTime = 0, drawCostTime = 0, drawTotalTime = 30;</p><p>private float x, y;<br />private int screenWidth = 480, screenHeight = 800;</p><p>public GameSurfaceView(Context context) {<br />super(context);</p><p>mActivity = (Activity01)context;</p><p>mHolder = this.getHolder();<br />mHolder.addCallback(this);<br />setFocusable(true);<br />}</p><p>@Override<br />public void surfaceCreated(SurfaceHolder holder) {<br />mPaint = new Paint();<br />mPaint.setColor(Color.CYAN);<br />mLoop = true;</p><p>new Thread(this).start();<br />}</p><p>@Override<br />public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {<br />screenWidth = width;<br />screenHeight = height;</p><p>x = screenWidth / 2;<br />y = screenHeight / 2;<br />}</p><p>@Override<br />public void surfaceDestroyed(SurfaceHolder holder) {<br />mLoop = false;<br />}</p><p>@Override<br />public void run() {<br />while(mLoop){<br />drawStartTime = SystemClock.uptimeMillis();</p><p>synchronized (mHolder) {<br />try {<br />mCanvas = mHolder.lockCanvas();<br />onDraw();// repaint<br />} catch (Exception e) {<br />e.printStackTrace();<br />} finally {<br />if(mCanvas != null){<br />mHolder.unlockCanvasAndPost(mCanvas);<br />}<br />}<br />}</p><p>drawCostTime = SystemClock.uptimeMillis() - drawStartTime;<br />Log.i("drawCostTime", "drawCostTime = " + drawCostTime);<br />try {<br />if(drawCostTime < drawTotalTime){<br />Thread.sleep(drawTotalTime - drawCostTime);// sleep elapse time<br />}<br />} catch (Exception e) {<br />e.printStackTrace();<br />}<br />}<br />}</p><p>private void onDraw(){<br />if(mHolder == null && mPaint == null){<br />return;<br />}</p><p>mPaint.setAntiAlias(true);<br />mPaint.setAlpha(100);</p><p>mPaint.setColor(Color.CYAN);// paint background color<br />mCanvas.drawRect(0, 0, screenWidth, screenHeight, mPaint);</p><p>mPaint.setColor(Color.BLUE);// paint foreground color<br />mCanvas.drawCircle(x, y, 50, mPaint);<br />}</p><p>@Override<br />public boolean onTouchEvent(MotionEvent event) {<br />super.onTouchEvent(event);</p><p>switch (event.getAction()) {<br />case MotionEvent.ACTION_DOWN:<br />this.x = event.getX();<br />this.y = event.getY();<br />break;</p><p>case MotionEvent.ACTION_MOVE:<br />this.x = event.getX();<br />this.y = event.getY();<br />break;</p><p>case MotionEvent.ACTION_UP:<br />this.x = event.getX();<br />this.y = event.getY();<br />break;</p><p>default:<br />this.x = event.getX();<br />this.y = event.getY();<br />break;<br />}</p><p>return true;// true then event can be implemented again<br />}</p><p>@Override<br />public boolean onKeyDown(int keyCode, KeyEvent event){<br />super.onKeyDown(keyCode, event);</p><p>if(keyCode == KeyEvent.ACTION_DOWN){<br />this.mActivity.finish();<br />}</p><p>return true;<br />}<br />}
運行效果:
源碼下載