flex添加 播放 gif圖片

初學flex遇到加載gif問題,翻遍網絡(由於google被封)找不到解決方法。總結整理後得到如下方法解決:


Flex裏Image控件插入gif動畫的圖片默認只顯示一幀,也就是說是不會動的,我們可以藉助GifPlayer類包來實現Flex裏插入動畫的Gif圖片。

GifPlayer下載:http://download.csdn.net/detail/d294083434/9432178如圖,提取org文件夾並放在程序目錄src下面就可以了。



實例一:簡單添加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.         <![CDATA[ 
  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.         <![CDATA[ 
  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>  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章