flex添加 播放 gif图片

初学flex遇到加载gif问题,翻遍网络(由于google被封)找不到解决方法。总结整理后得到如下方法解决:


Flex里Image控件插入gif动画的图片默认只显示一帧,也就是说是不会动的,我们可以借助GifPlayer类包来实现Flex里插入动画的Gif图片。

GifPlayer下载:http://download.csdn.net/detail/d294083434/9432178,如图,提取org文件夹并放在程序目录src下面就可以了。

flex添加 播放 gif图片_第1张图片

flex添加 播放 gif图片_第2张图片

实例一:简单添加gif

[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">  
  5.     <fx:Script>  
  6.          
  7.         import org.bytearray.gif.player.GIFPlayer; 
  8.              
  9.         private var gifPlay:GIFPlayer=new GIFPlayer(); 
  10.         private function init():void{ 
  11.         var req:URLRequest=new URLRequest("../flex_src/images/复件-雨量图标.gif"); 
  12.         gifPlay.load(req); 
  13.         gifImg.addChild(gifPlay);   
  14.         }  
  15.         ]]>  
  16.     fx:Script>  
  17.     <mx:TitleWindow title="abc" id="ADwindow" width="1000" height="800" >  
  18.         <mx:Image id="gifImg" width="221" height="92" creationComplete="init()"/>  
  19.      mx:TitleWindow>  
  20. s:Application>  


实例二:演示了几个常用的功能:播放、停止、到第几帧播放、到第几帧停止。

[html]  view plain  copy
  1. xml version="1.0" encoding="utf-8"?>  
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"   
  3.                xmlns:s="library://ns.adobe.com/flex/spark"   
  4.                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">  
  5.     <fx:Script>  
  6.          
  7.             import flash.net.URLRequest;   
  8.             import org.bytearray.gif.player.GIFPlayer;   
  9.             import org.bytearray.gif.events.FileTypeEvent;   
  10.             import org.bytearray.gif.events.GIFPlayerEvent;   
  11.             import org.bytearray.gif.events.FrameEvent;   
  12.             import org.bytearray.gif.events.TimeoutEvent;   
  13.             private var _myGIFPlayer:GIFPlayer = new GIFPlayer();//实例化GIFPlayer实例   
  14.             private var _totalFrame:Number;//总帧数   
  15.             //private var _currentFrame:Number;   
  16.              
  17.             private function init():void    
  18.             {   
  19.                 var request:URLRequest = new URLRequest("../flex_src/images/复件-雨量图标.gif");//同级目录下得md.gif图片   
  20.                 _myGIFPlayer.load(request);   
  21.                 img.addChild(_myGIFPlayer);//img控件添加播放   
  22.                 _myGIFPlayer.addEventListener(GIFPlayerEvent.COMPLETE, onCompleteGIF);//载入gif图片完毕时的回调函数   
  23.                 _myGIFPlayer.addEventListener(FrameEvent.FRAME_RENDERED, onFrameRendered);//读取帧的回调函数   
  24.                 //_myGIFPlayer.addEventListener(TimeoutEvent.TIME_OUT, onTimeOut);   
  25.             }   
  26.              
  27.             private function onCompleteGIF(event:GIFPlayerEvent):void    
  28.             {   
  29.                 _totalFrame = _myGIFPlayer.totalFrames;//总帧数   
  30.                 totalframe.text = String(_totalFrame);   
  31.             }   
  32.              
  33.             private function onFrameRendered(event:FrameEvent):void    
  34.             {   
  35.                 currentframe.text = String(_myGIFPlayer.currentFrame);//当前播放的帧数   
  36.             }   
  37.              
  38.             private function onTimeOut(event:TimeoutEvent):void    
  39.             {   
  40.                 trace ("gif is error!");   
  41.             }   
  42.              
  43.             private function play():void    
  44.             {   
  45.                 _myGIFPlayer.play();   
  46.             }   
  47.              
  48.             private function stop():void    
  49.             {   
  50.                 _myGIFPlayer.stop();   
  51.             }   
  52.              
  53.             private function gotoandplay():void    
  54.             {   
  55.                 var numFrame:Number = Math.floor(Math.random() * _totalFrame ) + 1;   
  56.                 _myGIFPlayer.gotoAndPlay(numFrame);   
  57.                 gotoplaybtn.label = "gotoPlay(" + numFrame + ")";   
  58.             }   
  59.              
  60.             private function gotoandstop():void    
  61.             {   
  62.                 var numFrame:Number = Math.floor(Math.random() * _totalFrame ) + 1;   
  63.                 _myGIFPlayer.gotoAndStop(numFrame);   
  64.                 gotostopbtn.label = "gotoStop(" + numFrame + ")";   
  65.             }  
  66.              
  67.         ]]>  
  68.     fx:Script>  
  69.     <s:layout>  
  70.         <s:BasicLayout/>  
  71.     s:layout>  
  72.     <fx:Declarations>  
  73.           
  74.     fx:Declarations>  
  75.     <mx:Image id="img" width="217" height="300" top="30" left="240"/>    
  76.       
  77.     <mx:ApplicationControlBar width="80%" bottom="20" horizontalCenter="0">    
  78.         <mx:Button label="Play" height="22" click="play();" styleName="primaryButton" />    
  79.         <mx:Button label="Stop" height="22" click="stop();" />    
  80.         <mx:Button id="gotoplaybtn" height="22" label="gotoPlay(rand)" click="gotoandplay();" />    
  81.         <mx:Button id="gotostopbtn" height="22" label="gotoStop(rand)" click="gotoandstop();" />    
  82.         <mx:Label text="TotalFrame:" />    
  83.         <mx:Label id="totalframe" />    
  84.         <mx:Label text="CurrentFrame:" />    
  85.         <mx:Label id="currentframe" />    
  86.     mx:ApplicationControlBar>    
  87. s:Application>  

你可能感兴趣的:(flex添加 播放 gif图片)