热门标签 | 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程序设计有所帮助。


推荐阅读
  • 深入解析Android自定义View面试题
    本文探讨了Android Launcher开发中自定义View的重要性,并通过一道经典的面试题,帮助开发者更好地理解自定义View的实现细节。文章不仅涵盖了基础知识,还提供了实际操作建议。 ... [详细]
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 本文介绍了如何使用jQuery根据元素的类型(如复选框)和标签名(如段落)来获取DOM对象。这有助于更高效地操作网页中的特定元素。 ... [详细]
  • 本文将详细介绍如何使用剪映应用中的镜像功能,帮助用户轻松实现视频的镜像效果。通过简单的步骤,您可以快速掌握这一实用技巧。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍如何在 Xcode 中使用快捷键和菜单命令对多行代码进行缩进,包括右缩进和左缩进的具体操作方法。 ... [详细]
  • 本文介绍了一款用于自动化部署 Linux 服务的 Bash 脚本。该脚本不仅涵盖了基本的文件复制和目录创建,还处理了系统服务的配置和启动,确保在多种 Linux 发行版上都能顺利运行。 ... [详细]
  • 在Linux系统中配置并启动ActiveMQ
    本文详细介绍了如何在Linux环境中安装和配置ActiveMQ,包括端口开放及防火墙设置。通过本文,您可以掌握完整的ActiveMQ部署流程,确保其在网络环境中正常运行。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 如何在WPS Office for Mac中调整Word文档的文字排列方向
    本文将详细介绍如何使用最新版WPS Office for Mac调整Word文档中的文字排列方向。通过这些步骤,用户可以轻松更改文本的水平或垂直排列方式,以满足不同的排版需求。 ... [详细]
  • 本文总结了在使用Ionic 5进行Android平台APK打包时遇到的问题,特别是针对QRScanner插件的改造。通过详细分析和提供具体的解决方法,帮助开发者顺利打包并优化应用性能。 ... [详细]
  • 理解存储器的层次结构有助于程序员优化程序性能,通过合理安排数据在不同层级的存储位置,提升CPU的数据访问速度。本文详细探讨了静态随机访问存储器(SRAM)和动态随机访问存储器(DRAM)的工作原理及其应用场景,并介绍了存储器模块中的数据存取过程及局部性原理。 ... [详细]
  • 360SRC安全应急响应:从漏洞提交到修复的全过程
    本文详细介绍了360SRC平台处理一起关键安全事件的过程,涵盖从漏洞提交、验证、排查到最终修复的各个环节。通过这一案例,展示了360在安全应急响应方面的专业能力和严谨态度。 ... [详细]
  • 几何画板展示电场线与等势面的交互关系
    几何画板是一款功能强大的物理教学软件,具备丰富的绘图和度量工具。它不仅能够模拟物理实验过程,还能通过定量分析揭示物理现象背后的规律,尤其适用于难以在实际实验中展示的内容。本文将介绍如何使用几何画板演示电场线与等势面之间的关系。 ... [详细]
  • 本文介绍如何通过Windows批处理脚本定期检查并重启Java应用程序,确保其持续稳定运行。脚本每30分钟检查一次,并在需要时重启Java程序。同时,它会将任务结果发送到Redis。 ... [详细]
author-avatar
MK小绿_136
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有