Android实现完整游戏循环的方法
更新时间:2015年10月10日 11:56:03 作者:红薯
这篇文章主要介绍了Android实现完整游戏循环的方法,以实例代码形式较为详细的分析了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 Dialog仿ios9中UIAlertController控件
这篇文章主要为大家详细介绍了Android Dialog仿ios9中UIAlertController控件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2018-06-06Flutter自定义下拉刷新时的loading样式的方法详解
Flutter中的下拉刷新,我们通常RefreshIndicator,可以通过color或strokeWidth设置下拉刷新的颜色粗细等样式,但如果要自定义自己的widget,RefreshIndicator并没有暴露出对应的属性,那如何修改呢,文中给大家介绍的非常详细,需要的朋友可以参考下2024-01-01解决android报错:Intel HAXM is required to run this AVD
这篇文章主要介绍了解决android报错:Intel HAXM is required to run this AVD,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-11-11Android Studio使用recyclerview实现展开和折叠功能(在之前的微信页面基础之上)
这篇文章主要介绍了Android Studio使用recyclerview实现展开和折叠(在之前的微信页面基础之上),本文通过截图实例代码给大家讲解的非常详细,需要的朋友可以参考下2020-03-03Android编程实现实时监听EditText文本输入的方法
这篇文章主要介绍了Android编程实现实时监听EditText文本输入的方法,结合实例形式分析了EditText控件及事件响应相关操作技巧,需要的朋友可以参考下2017-06-06
最新评论