我想创建一个倒计时60秒的GIF。我可以使用Photoshop,但不想经历为每个数字创建新图层的麻烦。
我正在寻找一种方法来自动生成GIF(或者我可以在事后合并为GIF的图像),从60倒数到0。
我会接受满足这些要求的任何答案。
答案 0 :(得分:2)
我将此AIR代码作为教育练习发布给读者。基本思想是使用ActionScript通过TextField
中的Sprite
个字段来呈现文本,使用Flash能够将任何DisplayObject渲染为静态位图数据,然后使用第三方打开 - source lib将每个渲染帧转换为gif。
注意:您可以将每个BitmapData帧保存到文件中,以便使用外部gif创建工具。
package {
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.utils.ByteArray;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.filesystem.FileMode;
import org.bytearray.gif.encoder.GIFEncoder;
import org.bytearray.gif.player.GIFPlayer;
public class Main extends Sprite {
var defaultFormat:TextFormat = new TextFormat();
var background:Sprite = new Sprite();
var countdownText = new TextField();
var fsize:int = 125;
var xsize:int = 100;
var ysize:int = 100;
public function Main():void {
defaultFormat.font = "Arial";
defaultFormat.size = fsize;
defaultFormat.color = 0xffffff;
var encoder:GIFEncoder = new GIFEncoder();
encoder.setRepeat(0);
encoder.setDelay(1000);
encoder.start();
setupCounterDisplay();
var startFrom:uint = 60;
var startColor:uint = 255;
for (var i:int = startFrom; i > -1; i--) {
var colorRGB:uint = (startColor / startFrom) * i;
encoder.addFrame(createCounterDisplay(i, ( colorRGB << 16 ) | ( colorRGB << 8 ) | colorRGB ) );
}
encoder.finish();
removeChild(background);
saveGIF("CounterDown.gif", encoder.stream);
playGIF(encoder.stream);
}
private function playGIF(data:ByteArray):void {
data.position = 0;
var player:GIFPlayer = new GIFPlayer();
player.loadBytes(data);
addChild(player);
}
private function saveGIF(fileName:String, data:ByteArray):void {
var outFile:File = File.desktopDirectory;
outFile = outFile.resolvePath(fileName);
var outStream:FileStream = new FileStream();
outStream.open(outFile, FileMode.WRITE);
outStream.writeBytes(data, 0, data.length);
outStream.close();
}
private function padString(string:String, padChar:String, finalLength:int, padLeft:Boolean = true):String {
while (string.length < finalLength) {
string = padLeft ? padChar + string : string + padChar;
}
return string;
}
private function setupCounterDisplay():void {
var xsize:int = 100;
var ysize:int = 100;
background.graphics.beginFill(0x000000, 1);
background.graphics.drawCircle(xsize, ysize, ysize);
background.graphics.endFill();
countdownText.defaultTextFormat = defaultFormat;
countdownText.border = true;
countdownText.borderColor = 0xff0000;
background.addChild(countdownText);
this.addChild(background);
}
private function createCounterDisplay(num:int, color:uint):BitmapData {
background.graphics.beginFill(0x000000, 1);
background.graphics.drawCircle(xsize, ysize, ysize);
background.graphics.endFill();
defaultFormat.color = color;
countdownText.defaultTextFormat = defaultFormat;
countdownText.text = padString(num.toString(), "0", 2);
countdownText.autoSize = "center";
countdownText.x = countdownText.width / 5;
countdownText.y = countdownText.height / 5;
var bitmap:BitmapData = new BitmapData(countdownText.width * 1.5, countdownText.height * 1.5, true);
bitmap.draw(background);
return bitmap;
}
}
}