时间:2021-05-20
本文实例讲述了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程序设计有所帮助。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android编程实现ListView内容无限循环显示的方法。分享给大家供大家参考,具体如下:其实要达到无限循环显示,主要就是实现继承Adapte
本文实例讲述了Android基本游戏循环。分享给大家供大家参考。具体如下://desiredfpsprivatefinalstaticintMAX_FPS=50
本文实例讲述了Android编程实现VideoView循环播放功能的方法。分享给大家供大家参考,具体如下:packagecom.hangcheng;import
猜数字小游戏,供大家参考,具体内容如下1、实验目的掌握循环、分支等结构的使用方法,以及数据类型转换的方法。2、实验内容设计并实现一款猜数字游戏。游戏开始之后,首
对Android利用ViewPager实现图片可以左右循环滑动效果,感兴趣的朋友可以直接点击查看内容详情。主要介绍如何实现ViewPager自动播放,循环滚动的