This article is an example of how Android implements the full game loop. Share to everyone for your reference. Specifically as follows:
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; The public class Droidzactivity extends activity {/** called the ' when ' is the ' The activity ' is a.
Tring 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 screens
}
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 {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 Ontoucheve NT (Motionevent event) {return Super.onTouchEvent (event);
} @Override protected void OnDraw (Canvas Canvas) {}}
I hope this article will help you with your Android program.