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


推荐阅读
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 【Windows】实现微信双开或多开的方法及步骤详解
    本文介绍了在Windows系统下实现微信双开或多开的方法,通过安装微信电脑版、复制微信程序启动路径、修改文本文件为bat文件等步骤,实现同时登录两个或多个微信的效果。相比于使用虚拟机的方法,本方法更简单易行,适用于任何电脑,并且不会消耗过多系统资源。详细步骤和原理解释请参考本文内容。 ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
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社区 版权所有