編輯:關於Android編程
本文實例講述了Android實現完整游戲循環的方法。分享給大家供大家參考。具體如下:
1. DroidzActivity.java:
package net.obviam.droidz; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Window; import android.view.WindowManager; public class DroidzActivity extends Activity { /** Called when the activity is first created. */ private static final String TAG = DroidzActivity.class.getSimpleName(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requesting to turn the title OFF requestWindowFeature(Window.FEATURE_NO_TITLE); // making it full screen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // set our MainGamePanel as the View setContentView(new MainGamePanel(this)); Log.d(TAG, "View added"); } @Override protected void onDestroy() { Log.d(TAG, "Destroying..."); super.onDestroy(); } @Override protected void onStop() { Log.d(TAG, "Stopping..."); super.onStop(); } }
2. MainThread.java:
package net.obviam.droidz; import android.util.Log; import android.view.SurfaceHolder; public class MainThread extends Thread { private static final String TAG = MainThread.class.getSimpleName(); private SurfaceHolder surfaceHolder; private MainGamePanel gamePanel; private boolean running; public void setRunning(boolean running) { this.running = running; } public MainThread(SurfaceHolder surfaceHolder, MainGamePanel gamePanel) { super(); this.surfaceHolder = surfaceHolder; this.gamePanel = gamePanel; } @Override public void run() { long tickCount = 0L; Log.d(TAG, "Starting game loop"); while (running) { tickCount++; // update game state // render state to the screen } Log.d(TAG, "Game loop executed " + tickCount + " times"); } }
3. MainGamePanel.java:
package net.obviam.droidz; import android.content.Context; import android.graphics.Canvas; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback { private MainThread thread; public MainGamePanel(Context context) { super(context); getHolder().addCallback(this); // create the game loop thread thread = new MainThread(); setFocusable(true); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { thread.setRunning(true); thread.start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { // try again shutting down the thread } } } @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } @Override protected void onDraw(Canvas canvas) { } }
希望本文所述對大家的Android程序設計有所幫助。
說起這個字母導航,我相信大家都不陌生,不論是聯系人列表還是城市列表,基本上都是需要字母導航,那我們就有必要來研究一下這個思路的探索了,畢竟這是一個非常常用的功能,如果現
本文詳細對Android中Serializable和Parcelable序列化對象進行學習,具體內容如下學習內容:1.序列化的目的2.Android中序列化的兩種方式3.
很多時候我們開發的軟件需要向用戶提供軟件參數設置功能,例如我們常用的QQ,用戶可以設置是否允許陌生人添加自己為好友。對於軟件配置參數的保存,如果是window軟件通常我們
上一篇文章總結的布局優化的問題,如果對布局優化不是很熟悉的,可以看一下Android Studido下的應用性能優化總結–布局優化 , 這周一直籌劃總結一下內