读书人

GIFPlayer的施用

发布时间: 2012-11-21 08:23:26 作者: rapoo

GIFPlayer的使用

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.

gifplayer用于播放gif图片,它通过urlrequest对象,或者图片的二进制对象加载gif。
Actionscript代码 收藏代码

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):

gif解码器,用于根据帧生成gif。
Actionscript代码 收藏代码

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.start(); //MUST HAVE!

for (var i:int = 0; i <= frames.length - 1; i++)
{
encoder.addFrame(frames[i]); //这里与原文稍有改动
encoder.setDelay(500);
}
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];
}
}
}

读书人网 >编程

热点推荐