[AS3]FPS帧数读取与检测的写法示例
[AS3]FPS帧数读取与检测的写法示例
[AS3]FPS帧数读取与检测的写法示例
- package
- {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.geom.Rectangle;
- import flash.system.System;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- import flash.utils.getTimer;
- public class Fps extends Sprite
- {
- private var fps:TextField;
- private var mem:TextField;
- private var instance:Fps;
- private var bitmapdata:BitmapData;
- private var i:int = 0;
- private var n:int = 10;
- private var diagramTimer:int;
- private var tfTimer:int;
- private var skins:int = -1;
- private var skinsChanged:int = 0;
- private static const diagramHeight:uint = 40;
- private static const diagramWidth:uint = 60;
- private static const maxMemory:uint = 500000000;
- public function Fps()
- {
- this.addEventListener(Event.ADDED_TO_STAGE,init);
- }
- public function init(e:Event):void
- {
- var bitmap:Bitmap = null;
- this.removeEventListener(Event.ADDED_TO_STAGE,init);
- fps = new TextField();
- mem = new TextField();
- if(instance == null)
- {
- fps.defaultTextFormat = new TextFormat("Tahoma", 10, 16711680);
- fps.autoSize = TextFieldAutoSize.LEFT;
- fps.text = "FPS:" + Number(stage.frameRate).toFixed(2);
- fps.x = -diagramWidth - 2;
- addChild(fps);
- mem.defaultTextFormat = new TextFormat("Tahoma", 10, 65280);
- mem.autoSize = TextFieldAutoSize.LEFT;
- mem.text = "MEM:" + byteToString(System.totalMemory);
- mem.x = -diagramWidth - 2;
- mem.y = fps.y + 10;
- addChild(mem);
- bitmapdata = new BitmapData(diagramWidth,diagramHeight,true,255);
- bitmap = new Bitmap(bitmapdata);
- bitmap.y = 24;
- bitmap.x = -diagramWidth;
- addChildAt(bitmap,0);
- addEventListener(Event.ENTER_FRAME,onEnterFrame);
- diagramTimer = getTimer();
- tfTimer = getTimer();
- }
- }
- private function onEnterFrame(e:Event):void
- {
- i++;
- if(i >= n)
- {
- i = 0;
- fps.text = "FPS: " + Number(1000 * n / (getTimer() - tfTimer)).toFixed(2);
- tfTimer = getTimer();
- }
- var _loc_2:* = 1000 / (getTimer() - diagramTimer);
- var _loc_3:* = _loc_2 > stage.frameRate ? (1) : (_loc_2 / stage.frameRate);
- diagramTimer = getTimer();
- bitmapdata.scroll(1, 0);
- bitmapdata.fillRect(new Rectangle(0, 0, 1, bitmapdata.height), 2852126720);
- bitmapdata.setPixel32(0, diagramHeight * (1 - _loc_3), 4294901760);
- mem.text = "MEM: " + byteToString(System.totalMemory);
- var ski:int = skins == 0 ? (0) : (skinsChanged / skins);
- bitmapdata.setPixel32(0, diagramHeight * (1 - ski), 4294967295);
- var meoryPer:Number = System.totalMemory / maxMemory;
- bitmapdata.setPixel32(0, diagramHeight * (1 - meoryPer), 4278255360);
- }
- private function byteToString(byte:uint):String
- {
- var byteStr:String = null;
- if (byte < 1024)
- {
- byteStr = String(byte) + "b";
- }
- else if (byte < 10240)
- {
- byteStr = Number(byte / 1024).toFixed(2) + "kb";
- }
- else if (byte < 102400)
- {
- byteStr = Number(byte / 1024).toFixed(1) + "kb";
- }
- else if (byte < 1048576)
- {
- byteStr = Math.round(byte / 1024) + "kb";
- }
- else if (byte < 10485760)
- {
- byteStr = Number(byte / 1048576).toFixed(2) + "mb";
- }
- else if (byte < 104857600)
- {
- byteStr = Number(byte / 1048576).toFixed(1) + "mb";
- }
- else
- {
- byteStr = Math.round(byte / 1048576) + "mb";
- }
- return byteStr;
- }
- }
- }
- package{
- import flash.display.Sprite
- import flash.utils.Timer;
- import flash.events.Event;
- import flash.events.TimerEvent;
- import flash.text.TextField;
- public class Fps extends Sprite{
- private var txt:TextField;
- private var count:int=0
- public function Fps(){
- init()
- }
- private function init():void{
- txt=new TextField();//极酷播放器提示:创建文本实例
- txt.textColor=0xff0000;//设置文本颜色
- addChild(txt)//加载这个文本
- var myTimer:Timer = new Timer(1000);//Timer类挺好使,
//类似于setInterval,参数是循环间隔时间,单位是毫秒- myTimer.addEventListener("timer", timerHandler);//注册事件
- this.addEventListener("enterFrame",countHandler)//注册事件,2.0的onEnterFrame
- myTimer.start();//Timer实例需要start来进行启动
- }
- private function timerHandler(event:TimerEvent):void{//Timer实例调用的方法
- txt.text="FPS:"+count
- count=0//每隔1秒进行清零
- }
- private function countHandler(event:Event):void{//真循环调用的方法
- count++//数值递加
- }
- }
- }
热门文章推荐
- [HLS]做自己的m3u8点播系统使用HTTP Live Streaming(HLS技术)
- [FMS]FMS流媒体服务器配置与使用相关的介绍
- [AS3]什么是M3U8,与HTML5的区别是什么
- AS2.0 让flash自适应全屏,并且不自动缩放
- [AS3]as3.0的sound类常用技巧整理
- [AS3]as3与ByteArray详解、ByteArray介绍、ByteArray用法
- 关于RTMP,RTMPT,RTMPS,RTMPE,RTMPTE协议的介绍
- [JS]分享浏览器弹出窗口不被拦截JS示例
请稍候...