flex gifloader

来源:互联网 发布:老汉卖羊c语言 编辑:程序博客网 时间:2024/05/15 23:47

最近项目需要运用到FLEX,但GOOGLE一下发现可用的资料实在太少了。

找了好久发现google code上在这个还不错,还有很多东西可以好好研究研究

 http://code.google.com/p/as3gif/

 

 

下面的例子是将动态GIF绑定到SPRITE上

 

package com.render
{
 import flash.display.Sprite;
 import flash.events.IOErrorEvent;
 import flash.net.URLRequest;
 
 import mx.core.Application;
 
 import org.bytearray.gif.player.GIFPlayer;

 public class SpriteWithBitmap extends Sprite
 {
  //Pass the source path or url here.
  private static const defaultUrl:String = "/consoleweb/console/images/treewait.gif";
 
  public function SpriteWithBitmap(url:String = defaultUrl)
  {
   loadImg(url);
  }
  private function loadImg(url:String):void
  {
   var gifPlayer:GIFPlayer = new GIFPlayer();
   var request:URLRequest = new URLRequest(url);
   gifPlayer.load(request);
   gifPlayer.addEventListener(IOErrorEvent.IO_ERROR,loadFailure);
   
   this.addChild(gifPlayer);
   
   //设置图片居中显示
   gifPlayer.x = Application.application.checkTree.width * 0.5 - 32;
   gifPlayer.y = Application.application.checkTree.height * 0.5 -32;
  }
 
  private function loadFailure(event:IOErrorEvent):void
  {
   
  }
 }
}

 

但要动态取到GIF的宽度还是没有头绪,在网上看到过这个

Hi, Thibault Imbert. The width/height property return 0, event after COMPLETE event dispatch. The GIFPlayerEvent has the ‘rect’, but it’s useless.
I’ve edited your GIFPlayer class (you should have done that).
All u needed 2 do is just to create a public var

public var rect:Rectangle;

And after in the readStream method to do so:

rect = aFrames[0].bitmapData.rect;
dispatchEvent (new GIFPlayerEvent( GIFPlayerEvent.COMPLETE , aFrames[0].bitmapData.rect));

U’d probably ask why did i do that. The answer is simple – 2 make thingz more useful.

Ex:

import org.gif.player.GIFPlayer;
import org.gif.events.GIFPlayerEvent;

var myGIFPlayer:GIFPlayer = new GIFPlayer();
myGIFPlayer.addEventListener(GIFPlayerEvent.COMPLETE, onCompleteGIFLoad);
myGIFPlayer.load(new URLRequest(“diego.gif”));
addChild(myGIFPlayer);

function onCompleteGIFLoad(event:GIFPlayerEvent):void
{
updatePos();
}

function updatePos():void
{
trace(“Now we can retrive the width/height in order to place/replace the player”, myGIFPlayer.rect);
}

 

在上述方法中设置this.x  this.y就可以改变gif的位置了