Android测量每秒帧数Frames Per Second (FPS)的方法

本文实例讲述了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 < MAX_FRAME_SKIPS) { // we need to catch up this.gamePanel.update(); // update without rendering sleepTime += FRAME_PERIOD; // add frame period to check if in next frame framesSkipped++; } if (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 < FPS_HISTORY_NR; i++) { totalFps += fpsStore[i]; } // obtain the average if (statsCount < FPS_HISTORY_NR) { // in case of the first 10 triggers averageFps = totalFps / statsCount; } else { averageFps = totalFps / FPS_HISTORY_NR; } // saving the number of total frames skipped totalFramesSkipped += framesSkippedPerStatCycle; // resetting the counters after a status record (1 sec) framesSkippedPerStatCycle = 0; statusIntervalTimer = 0; frameCountPerStatCycle = 0; statusIntervalTimer = System.currentTimeMillis(); lastStatusStore = statusIntervalTimer; // Log.d(TAG, "Average FPS:" + df.format(averageFps)); gamePanel.setAvgFps("FPS: " + df.format(averageFps)); } } private void initTimingElements() { // initialise timing elements fpsStore = new double[FPS_HISTORY_NR]; for (int i = 0; i < FPS_HISTORY_NR; i++) { fpsStore[i] = 0.0; } Log.d(TAG + ".initTimingElements()", "Timing elements for stats initialised"); } }

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

时间: 2024-10-28 16:45:03

Android测量每秒帧数Frames Per Second (FPS)的方法的相关文章

Android测量每秒帧数Frames Per Second (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 impa

禁用Wi-Fi是否可以阻止Android手机发送无线帧?

本文讲的是禁用Wi-Fi是否可以阻止Android手机发送无线帧?, 前言 移动智能手机发出的Wi-Fi信号可以被某些机构和别有用心的人利用,跟踪用户的移动行为,所以你如果关闭移动设备的Wi-Fi接口则意味着你不想被人发现. Android系统具有设置启用或禁用Wi-Fi功能的选项,然而,你以为只要通过禁用Wi-Fi选项就足以阻止你手机的所有Wi-Fi活动吗?大错特错.为此,我还专门进行了一些测试,以验证这种说法的准确性. Android手机的Wi-Fi配置 Android手机的Wi-Fi扫描

FPS Meter:手机帧数显示软件

在PC平台上有一款很流行的帧数捕获及显示软件Fraps,很多游戏玩家都会用它来监测自己电脑的游戏性能.而 Android平台上也有具有类似功能的软件FPS Meter Root,可以显示出手机正在运行的软件的帧数,甚至还可以通过指定的记录软件录制视频.FPS Meter Root的界面非常简单,只有像字体大小.位置和透明度等几个选项,以及一个巨大的按钮,按下它就可以激活FPS计数器了.FPS Meter Root界面截图目前FPS Meter Root只能运行在Ice Cream Sandwic

电脑帧数很低怎么办

一般情况电脑配置没有达到要求可能会造成电脑玩哟写高配置游戏出现卡屏幕或者游戏界面跟不上的情况,所有首先需要保证电脑配置必须完全支持游戏的运行才可以的.比如一般玩魔兽世界.CF游戏显卡还是集成的显卡的话一般就支持运行不了的哦.唯一的解决方法就是更换加强配置了. 如果电脑配置达到的了要求还是出现帧数很低的情况的话,那解决方法如下: 电脑常识 1.首先对电脑不要进行超频的操作,保存系统推荐的稳定运行,然后尝试将电脑元件cpu.内存和主板总线频率在内的可调节元件参数降低一个档次,一般在购买电脑的主板说明

wallpaper engine帧数怎么设置?

wallpaper engine帧数怎么设置?   笔者的设置 效果一览 如果你的配置(显卡)还可以,10-25是不错的,神机可以无视一切开60帧.

php读取flash文件高宽帧数背景颜色的方法

  本文实例讲述了php读取flash文件高宽帧数背景颜色的方法.分享给大家供大家参考. 具体实现方法如下: 代码如下: /* 示例: $file = '/data/ad_files/5/5.swf'; $flash = new flash(); $flash = $flash->getswfinfo($file); echo " 文件的宽高是:".$flash["width"].":".$info["height"];

暗黑3电脑卡,帧数过低怎么办

玩暗黑3时电脑卡,帧数过低怎么办?本为大家总结了时下暗黑3卡帧数.帧数过低的全部解决方式,希望可以帮到大家. 电脑常识   其他更多方式 1.确保开启你的独显.屏幕右下角可以看 2.确保电源选项是高性能.供电不足会导致性能下降 3.确保散热选择强散热.温度过高会导致降频 4.查看驱动是否更新.最好先备份,新驱动可能不稳定,可以使用驱动精灵等软件. 5.如果还是有问题请试试看修改暴雪硬件识别等级 修改兼容性 6.硬盘参数修改

解决Fraps在win7或vista下无法显示帧数的问题

本人自从Vista升到Win7之后就没有正常使用过Fraps,无法在游戏中显示帧数,用了很多版本都不行,在网上也没有找到解决方法 前两天刚刚装了正式版的Win7,64位的,持侥幸心理安装了Fraps,运行PES 2009测试了一下,可以正常显示,但是第二天开机之后又不行了,不管什么游戏都不行. 遂自己研究,偶然情况下发现,使用管理员身份运行游戏之后就可以正常显示帧数了(方法不用教吧?右键单击快捷方式,然后点"以管理员身份运行"),希望能帮上那些Vista和Win7系统下无法正常使用Fr

采用AMD双显卡交火、叠加机型对于游戏帧数的影响

使用AMD显卡交火技术的机型,在正常运行大型3D游戏过程中会遇到卡顿的现象. 原因分析: 此类现象出现的原因是游戏对于AMD Dual Graphcis双显卡共同工作技术不兼容导致. 解决方案: 首先,请右击桌面任意空白位置,选择"显示卡属性"选项,打开AMD显卡控制台,如下图: 请点击"首选项"按钮 打开"首选项"菜单,如下图,将视图模式改为"高级视图"; 第二步,请右击左侧的功能栏中的"性能"选项卡,找