Android SurfaceView遊戲開發樣本

來源:互聯網
上載者:User

當我們需要開發一個複雜遊戲的時候,而且對程式的執行效率要求很高時,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 />}
運行效果:

源碼下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.