flex 播放gif动画

今天闲来无事网上搜下flex播放gif动画        同时推荐一个不错的gif动画系列      史前动物    百度你就知道!

Flex里Image控件插入gif动画的图片默认只显示一帧,也就是说是不会动的,我们可以借助GifPlayer类包来实现Flex里插入动画的Gif图片。
GifPlayer下载: http://code.google.com/p/as3gif/downloads/list
实例里演示了几个常用的功能:播放、停止、到第几帧播放、到第几帧停止。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
				layout="absolute" horizontalAlign="center"
				creationComplete="init();" preloader="com.preload.PreLoad" 
				backgroundColor="0x414141" > 
	<mx:Style source="flex/yfskin/yflexskin.css" />
	<mx:Script>
		<![CDATA[
			import flash.net.URLRequest;
			import org.gif.player.GIFPlayer;
			import org.gif.events.FileTypeEvent;
			import org.gif.events.GIFPlayerEvent;
			import org.gif.events.FrameEvent;
			import org.gif.events.TimeoutEvent;
			private var _myGIFPlayer:GIFPlayer = new GIFPlayer();//实例化GIFPlayer实例
			private var _totalFrame:Number;//总帧数
			//private var _currentFrame:Number;
			
			private function init():void 
			{
				var request:URLRequest = new URLRequest("md.gif");//同级目录下得md.gif图片
				_myGIFPlayer.load(request);
				img.addChild(_myGIFPlayer);//img控件添加播放
				_myGIFPlayer.addEventListener(GIFPlayerEvent.COMPLETE, onCompleteGIF);//载入gif图片完毕时的回调函数
				_myGIFPlayer.addEventListener(FrameEvent.FRAME_RENDERED, onFrameRendered);//读取帧的回调函数
				//_myGIFPlayer.addEventListener(TimeoutEvent.TIME_OUT, onTimeOut);
			}
			
			private function onCompleteGIF(event:GIFPlayerEvent):void 
			{
				_totalFrame = _myGIFPlayer.totalFrames;//总帧数
				totalframe.text = String(_totalFrame);
			}
			
			private function onFrameRendered(event:FrameEvent):void 
			{
				currentframe.text = String(_myGIFPlayer.currentFrame);//当前播放的帧数
			}
			
			private function onTimeOut(event:TimeoutEvent):void 
			{
				trace ("gif is error!");
			}
			
			private function play():void 
			{
				_myGIFPlayer.play();
			}
			
			private function stop():void 
			{
				_myGIFPlayer.stop();
			}
			
			private function gotoandplay():void 
			{
				var numFrame:Number = Math.floor(Math.random() * _totalFrame ) + 1;
				_myGIFPlayer.gotoAndPlay(numFrame);
				gotoplaybtn.label = "gotoPlay(" + numFrame + ")";
			}
			
			private function gotoandstop():void 
			{
				var numFrame:Number = Math.floor(Math.random() * _totalFrame ) + 1;
				_myGIFPlayer.gotoAndStop(numFrame);
				gotostopbtn.label = "gotoStop(" + numFrame + ")";
			}
		]]>
	</mx:Script>
	
	<mx:Image id="img" width="217" height="300" top="30" left="240"/>
	
	<mx:ApplicationControlBar width="80%" left="50" bottom="20" >
		<mx:Button label="Play" height="22" click="play();" styleName="primaryButton" />
		<mx:Button label="Stop" height="22" click="stop();" />
		<mx:Button id="gotoplaybtn" height="22" label="gotoPlay(rand)" click="gotoandplay();" />
		<mx:Button id="gotostopbtn" height="22" label="gotoStop(rand)" click="gotoandstop();" />
		<mx:Label text="TotalFrame:" />
		<mx:Label id="totalframe" />
		<mx:Label text="CurrentFrame:" />
		<mx:Label id="currentframe" />
	</mx:ApplicationControlBar>
	
</mx:Application>

首先感谢网上前辈的代码 但是没说明下载下来的gifPlayer怎么用     研究下 下载后的东西就几百k  目录如下

flex 播放gif动画_第1张图片

把  org文件夹(71kb左右)  考到你的工程目录下保证引包时能找到就行了 例如

import org.gif.player.GIFPlayer;
接下来就是粘贴代码  和准备gif图片  发挥你的想象编写更炫的效果!

我写了个程序循环播放某一个文件夹下所有gif图片  但是当播放完一张后载入第二张图片时速度非常慢  不知道为什么?(难道是flex编译时把gif图片也编译进去了????)



你可能感兴趣的:(flex 播放gif动画)