Basic Usage
The GIFPlayer
The GIFPlayer class is used to play the gif animation.
It can load gif file from an URLRequest object or a ByteArray object.
Example:
package
{
import flash.display.Sprite;
import flash.net.URLRequest;
import org.gif.player.GIFPlayer;
public class Main extends Sprite
{
public function Main():void
{
var request:URLRequest = new URLRequest("diego.gif");
var player:GIFPlayer = new GIFPlayer();
player.load(request);
addChild(player);
}
}
}The GIFEncoder
The GIFEncoder class use to create a gif file by frames.
Example(draw two frames and add to GIFEncoder then play the gif data by GIFPlayer):
package
{
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.utils.ByteArray;
import org.gif.encoder.GIFEncoder;
import org.gif.player.GIFPlayer;
public class Main extends Sprite
{
public function Main():void
{
var frames:Array = createFrames();
var encoder:GIFEncoder = new GIFEncoder();
encoder.setRepeat(0); //AUTO LOOP
encoder.setDelay(500);
encoder.start(); //MUST HAVE!
encoder.addFrame(frames[0]);
encoder.addFrame(frames[1]);
encoder.finish(); //MUST HAVE!
playGIF(encoder.stream);
}
private function playGIF(data:ByteArray):void
{
data.position = 0;
var player:GIFPlayer = new GIFPlayer();
player.loadBytes(data);
addChild(player);
}
private function createFrames():Array
{
var shape:Shape = new Shape();
shape.graphics.lineStyle(1, 0);
shape.graphics.moveTo(60, 0);
shape.graphics.lineTo(60, 120);
var frame1:BitmapData = new BitmapData(120, 120);
frame1.draw(shape);
shape.graphics.clear();
shape.graphics.lineStyle(1, 0);
shape.graphics.moveTo(0, 60);
shape.graphics.lineTo(120, 60);
var frame2:BitmapData = new BitmapData(120, 120);
frame2.draw(shape);
return [frame1, frame2];
}
}
}
how to save the Gif file with the new flash player 10 FileReference? without a PHP or another server side script?
To Build an SWC file
compc -optimize=true -static-link-runtime-shared-libraries=true -source-path+=. -output=bin/as3gif_0.6.swc -include-classes org.bytearray.gif.decoder.GIFDecoder org.bytearray.gif.encoder.GIFEncoder org.bytearray.gif.encoder.LZWEncoder org.bytearray.gif.encoder.NeuQuant? org.bytearray.gif.errors.FileTypeError? org.bytearray.gif.events.FileTypeEvent? org.bytearray.gif.events.FrameEvent? org.bytearray.gif.events.GIFPlayerEvent org.bytearray.gif.events.TimeoutEvent? org.bytearray.gif.frames.GIFFrame org.bytearray.gif.player.GIFPlayer
and don't forget to include import org.bytearray.gif.player.GIFPlayer
<project name="as3gif" basedir="../" default="flex.compile">
<!-- env -->
<!-- flex defs --> <!-- compile flex --> </project>#Project options project.swc.name=as3gif project.description=as3gif project.version=0.6
previous 2 comments build.xml and cfg.properties for create an swc lib
To use in MXML: 1. Create Flex library project around GifPlayer? component:
package { import flash.net.URLRequest; import mx.core.UIComponent; import org.bytearray.gif.player.GIFPlayer; public class GIFPlayerComponent extends UIComponent { private var m_gif:GIFPlayer = new GIFPlayer(); private var _url:String; public function GIFPlayerComponent() { super(); addChild( m_gif ); } public function get url():String { return _url; } public function set url(value:String):void { _url = value; var urlReq:URLRequest = new URLRequest( _url ); m_gif.load( urlReq ); } } }2. Reference SWC in your MXML component and use the tag in this way:
I used the following build.xml file to create a SWC from the GIFPlayer-0.6 download -- it did not include a src dir or a SWC.
<project name="as3gif" basedir="." default="flex.compile"> <!-- env --> <property environment="env" /> <property file="${basedir}/ant/cfg.properties" /> <!-- flex defs --> <taskdef resource="flexTasks.tasks" classpath="${env.FLEX_HOME}/ant/lib/flexTasks.jar" /> <property name="FLEX_HOME" value="${env.FLEX_HOME}"/> <property name="asdoc.exec" value="${FLEX_HOME}/bin/asdoc" /> <property name="fx.base.dir" value="${basedir}"/> <property name="fx.src.dir" value="${basedir}" /> <property name="fx.lib.dir" value="${fx.base.dir}/lib" /> <property name="fx.dist.dir" value="${fx.base.dir}/dist" /> <!-- compile flex --> <target name="flex.compile"> <compc output="${fx.base.dir}/GIFPlayer-0.6.swc"> <include-sources dir="${fx.src.dir}/org/bytearray/gif/decoder" includes="*" /> <include-sources dir="${fx.src.dir}/org/bytearray/gif/encoder" includes="*" /> <include-sources dir="${fx.src.dir}/org/bytearray/gif/errors" includes="*" /> <include-sources dir="${fx.src.dir}/org/bytearray/gif/events" includes="*" /> <include-sources dir="${fx.src.dir}/org/bytearray/gif/frames" includes="*" /> <include-sources dir="${fx.src.dir}/org/bytearray/gif/player" includes="*" /> </compc> <echo>SWC created successfully</echo> </target> </project>In order to use embedded gifs
The following GIFPlayerComponent class can be used:
GIFPlayerComponent.as
package { import flash.net.URLRequest; import flash.utils.ByteArray; import mx.core.ByteArrayAsset; import mx.core.UIComponent; import org.bytearray.gif.player.GIFPlayer; /** * Wrapper for GIFPlayer. This class was created as suggested at: * * http://code.google.com/p/as3gif/wiki/How_to_use * */ public class GIFPlayerComponent extends UIComponent { private var m_gif : GIFPlayer = new GIFPlayer(); private var _url : String; private var _byteArray : ByteArray public function GIFPlayerComponent() { super(); this.addChild( m_gif ); } public function get url() : String { return _url; } public function set url(value : String) : void { _url = value; var urlReq : URLRequest = new URLRequest( _url ); m_gif.load(urlReq); } public function get byteArray() : ByteArray { return _byteArray } public function set byteArray(value : ByteArray) : void { _byteArray = value; m_gif.loadBytes(_byteArray); } } }EmbedAssets?.as
package { public class EmbedAssets { [Embed(source = "img/loading-small.gif", mimeType = "application/octet-stream")] public static var LOADING_SMALL : Class; } }