最新国产好看的视频,伊人天堂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
  }
}

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

相關(guān)文章

最新評論

金堂县| 喀喇| 安多县| 湛江市| 盐池县| 许昌市| 长宁县| 贡觉县| 阳信县| 都昌县| 大新县| 始兴县| 祁阳县| 荔波县| 灵山县| 长沙县| 政和县| 会宁县| 易门县| 射阳县| 尖扎县| 田阳县| 乌恰县| 平谷区| 平邑县| 岚皋县| 龙川县| 九寨沟县| 长寿区| 桃园县| 兴海县| 阿合奇县| 沧州市| 宜川县| 同心县| 都江堰市| 开鲁县| 察哈| 钟山县| 龙口市| 临泉县|