最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android基本游戲循環(huán)實(shí)例分析

 更新時(shí)間:2015年10月10日 12:09:49   作者:紅薯  
這篇文章主要介紹了Android基本游戲循環(huán),以完整實(shí)例形式較為詳細(xì)的分析了Android實(shí)現(xiàn)基本游戲循環(huán)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android基本游戲循環(huán)。分享給大家供大家參考。具體如下:

// desired fps
private final static int  MAX_FPS = 50;
// maximum number of frames to be skipped
private final static int  MAX_FRAME_SKIPS = 5;
// the frame period
private final static int  FRAME_PERIOD = 1000 / MAX_FPS; 
@Override
public void run() {
  Canvas canvas;
  Log.d(TAG, "Starting game loop");
  long beginTime;   // the time when the cycle begun
  long timeDiff;   // the time it took for the cycle to execute
  int sleepTime;   // ms to sleep (<0 if we're behind)
  int framesSkipped; // number of frames being skipped 
  sleepTime = 0;
  while (running) {
    canvas = null;
    // try locking the canvas for exclusive pixel editing
    // in the surface
    try {
      canvas = this.surfaceHolder.lockCanvas();
      synchronized (surfaceHolder) {
        beginTime = System.currentTimeMillis();
        framesSkipped = 0; // resetting the frames skipped
        // update game state
        this.gamePanel.update();
        // render state to the screen
        // draws the canvas on the panel
        this.gamePanel.render(canvas);
        // calculate how long did the cycle take
        timeDiff = System.currentTimeMillis() - beginTime;
        // calculate sleep time
        sleepTime = (int)(FRAME_PERIOD - timeDiff);
        if (sleepTime > 0) {
          // if sleepTime > 0 we're OK
          try {
            // send the thread to sleep for a short period
            // very useful for battery saving
            Thread.sleep(sleepTime);
          } catch (InterruptedException e) {}
        }
        while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
          // we need to catch up
          // update without rendering
          this.gamePanel.update();
          // add frame period to check if in next frame
          sleepTime += FRAME_PERIOD;
          framesSkipped++;
        }
      }
    } finally {
      // in case of an exception the surface is not left in
      // an inconsistent state
      if (canvas != null) {
        surfaceHolder.unlockCanvasAndPost(canvas);
      }
    }  // end finally
  }
}

希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

富民县| 揭西县| 庆元县| 西吉县| 大新县| 怀化市| 东乡县| 建昌县| 抚松县| 佛教| 武胜县| 东兰县| 泰和县| 宜都市| 温州市| 平谷区| 镇康县| 汕头市| 宁城县| 稻城县| 抚顺市| 柳州市| 灵寿县| 博兴县| 平原县| 喀喇| 盐边县| 沛县| 黔江区| 灵台县| 隆回县| 大兴区| 嘉定区| 灵川县| 施秉县| 苗栗县| 鄂托克前旗| 宁海县| 英德市| 和政县| 五原县|