热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

Android测量每秒帧数FramesPerSecond(FPS)的方法

这篇文章主要介绍了Android测量每秒帧数FramesPerSecond(FPS)的方法,涉及Android针对多媒体文件属性操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android测量每秒帧数Frames Per Second (FPS)的方法。分享给大家供大家参考。具体如下:

MainThread.java:

package net.obviam.droidz;
import java.text.DecimalFormat;
import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;
/**
 * @author impaler
 *
 * The Main thread which contains the game loop. The thread must have access to
 * the surface view and holder to trigger events every game tick.
 */
public class MainThread extends Thread {
 private static final String TAG = MainThread.class.getSimpleName();
 // 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;
 // Stuff for stats */
 private DecimalFormat df = new DecimalFormat("0.##"); // 2 dp
 // we'll be reading the stats every second
 private final static int STAT_INTERVAL = 1000; //ms
 // the average will be calculated by storing
 // the last n FPSs
 private final static int FPS_HISTORY_NR = 10;
 // last time the status was stored
 private long lastStatusStore = 0;
 // the status time counter
 private long statusIntervalTimer = 0l;
 // number of frames skipped since the game started
 private long totalFramesSkipped   = 0l;
 // number of frames skipped in a store cycle (1 sec)
 private long framesSkippedPerStatCycle = 0l;
 // number of rendered frames in an interval
 private int frameCountPerStatCycle = 0;
 private long totalFrameCount = 0l;
 // the last FPS values
 private double fpsStore[];
 // the number of times the stat has been read
 private long statsCount = 0;
 // the average FPS since the game started
 private double averageFps = 0.0;
 // Surface holder that can access the physical surface
 private SurfaceHolder surfaceHolder;
 // The actual view that handles inputs
 // and draws to the surface
 private MainGamePanel gamePanel;
 // flag to hold game state
 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() {
  Canvas canvas;
  Log.d(TAG, "Starting game loop");
  // initialise timing elements for stat gathering
  initTimingElements();
  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  0) {
      Log.d(TAG, "Skipped:" + framesSkipped);
     }
     // for statistics
     framesSkippedPerStatCycle += framesSkipped;
     // calling the routine to store the gathered statistics
     storeStats();
    }
   } finally {
    // in case of an exception the surface is not left in
    // an inconsistent state
    if (canvas != null) {
     surfaceHolder.unlockCanvasAndPost(canvas);
    }
   } // end finally
  }
 }
 /**
  * The statistics - it is called every cycle, it checks if time since last
  * store is greater than the statistics gathering period (1 sec) and if so
  * it calculates the FPS for the last period and stores it.
  *
  * It tracks the number of frames per period. The number of frames since
  * the start of the period are summed up and the calculation takes part
  * only if the next period and the frame count is reset to 0.
  */
 private void storeStats() {
  frameCountPerStatCycle++;
  totalFrameCount++;
  // check the actual time
  statusIntervalTimer += (System.currentTimeMillis() - statusIntervalTimer);
  if (statusIntervalTimer >= lastStatusStore + STAT_INTERVAL) {
   // calculate the actual frames pers status check interval
   double actualFps = (double)(frameCountPerStatCycle / (STAT_INTERVAL / 1000));
   //stores the latest fps in the array
   fpsStore[(int) statsCount % FPS_HISTORY_NR] = actualFps;
   // increase the number of times statistics was calculated
   statsCount++;
   double totalFps = 0.0;
   // sum up the stored fps values
   for (int i = 0; i 

希望本文所述对大家的java程序设计有所帮助。


推荐阅读
  • SwipeRefreshLayout 是一个常用的刷新控件,可以包裹一个可滑动的子控件(如 ListView 或 RecyclerView)以实现竖直滑动时的页面刷新。然而,它本身并不支持上拉加载更多。本文将介绍如何通过继承 SwipeRefreshLayout 来实现这一功能。 ... [详细]
  • HPE OEM Brocade 300 交换机无中断固件升级指南
    本文详细介绍了如何通过FTP方式对HPE OEM Brocade 300交换机进行无中断固件升级,确保网络服务的连续性。 ... [详细]
  • 本文介绍了如何使用 Gesture Detector 和 overridePendingTransition 方法来实现滑动界面和过渡动画。 ... [详细]
  • 驱动程序的基本结构1、Windows驱动程序中重要的数据结构1.1、驱动对象(DRIVER_OBJECT)每个驱动程序会有唯一的驱动对象与之对应,并且这个驱动对象是在驱 ... [详细]
  • 作为一名饼干爱好者,我尝试过各种各样的饼干。虽然威化饼和消化饼都有其独特的风味,但我对柠檬夹心饼干情有独钟。这种饼干不仅口感丰富,还带有清新的柠檬香味。 ... [详细]
  • 在 PHP 中,使用 `continue` 关键字结合数字可以有效地终止嵌套的 `foreach` 循环。本文将详细介绍如何使用 `continue` 加数字来控制不同层次的循环。 ... [详细]
  • 作为一名Android应用开发新手,我在尝试将MediaPlayer处理逻辑从MainActivity分离到另一个类时遇到了问题。尽管搜索了很长时间,但仍未找到满意的解决方案。 ... [详细]
  • 解决网页乱码问题的实用方法
    网页乱码问题在开发中较为常见,主要由文件编码、程序字符集设置和数据库连接字符集设置不当引起。本文将详细介绍如何逐一排查并解决这些问题。 ... [详细]
  • 本文介绍了如何使用开源工具ChkBugReport来解析和分析Android设备的Bugreport。ChkBugReport能够将复杂的Bugreport转换为易于阅读的HTML报告,并提供详细的图表和分析结论。 ... [详细]
  • 本文通过一个示例展示了如何使用HTML和CSS美化并实现响应式的按钮组。 ... [详细]
  • 本文介绍了三种解决 Git Push 冲突的方法,包括创建新分支、手动解决冲突和强行推送。这些方法适用于不同的开发场景,如版本迭代、多人协作和个人开发。 ... [详细]
  • Excel VBA自动化添加数字证书(续)
    本文继续探讨如何在Excel VBA中自动添加数字证书。上一篇文章因突发情况未能完成,本次将详细介绍证书的生成和集成方法。 ... [详细]
  • 本文介绍了 Oracle SQL 中的集合运算、子查询、数据处理、表的创建与管理等内容。包括查询部门号为10和20的员工信息、使用集合运算、子查询的注意事项、数据插入与删除、表的创建与修改等。 ... [详细]
  • 本文讲述了一位80后的普通男性程序员,尽管没有高学历,但通过不断的努力和学习,在IT行业中逐渐找到了自己的位置。从最初的仓库管理员到现在的多技能开发者,他的职业生涯充满了挑战与机遇。 ... [详细]
  • 本文详细介绍了 Android 开发中常用的单位 dip(设备独立像素)、px(像素)、pt(点)和 sp(可缩放像素),并解释了它们在不同屏幕密度下的应用。 ... [详细]
author-avatar
小女人hoffix_523
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有