Android to achieve full game cycle _android

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.