Flex里播放Gif图片
?
http://www.flexrain.cn/demo/flex/gifplayer.swf
?
Flex里Image控件插入gif动画的图片默认只显示一帧,也就是说是不会动的,我们可以借助GifPlayer类包来实现Flex里插入动画的Gif图片。
GifPlayer下载: http://code.google.com/p/as3gif/
实例里演示了几个常用的功能:播放、停止、到第几帧播放、到第几帧停止。
?
<?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(); private var _totalFrame:Number; //private var _currentFrame:Number; private function init():void { var request:URLRequest = new URLRequest("md.gif"); _myGIFPlayer.load(request); img.addChild(_myGIFPlayer); _myGIFPlayer.addEventListener(GIFPlayerEvent.COMPLETE, onCompleteGIF); _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>
?