首先介绍添加Gif动画的实现。Gif动画包括多帧图片和延时信息。建立Sprite,将Gif的每一帧图片及延时时间提取出来,放到Sprite的帧上并延时,再将此Sprite放到平台上,原SWF文件作为另一Sprite放置到它的后面(通过层来实现)。核心代码及注释如下:
swfw:=140;
swfh:=226;//SWF文件宽度和高度,本文以QQ大头贴为例。
Movie := TFlashMovie.Create(0, 0, swfw * twips, swfh* twips, 12);//建立影片,大小及帧速
Movie.Version := 6;
Movie.SystemCoord := scPix;//建立影片并设置版本号和单位
Gif := TGifImage.Create;
Gif.LoadFromFile(edit1.Text);
gifSprite := Movie.AddSprite;//建立GIF精灵
for i := 0 to Gif.Images.Count - 1 do//载入每一帧图片并放到舞台上
begin
image := Movie.AddImage('');
image.LoadDataFromHandle(Gif.Images.SubImages[i].Bitmap.Handle);
if Gif.IsTransparent then
image.SetAlphaColor(SWFRGBA(Gif.BackgroundColor, 0));
shape := movie.AddRectangle(0, 0, Gif.Width, Gif.Height);
shape.SetImageFill(image, fmFit);
with gifSprite.PlaceObject(shape, 0) do
begin
SetTranslate(strtoint(edit3.text),strtoint(edit4.text));//放置位置和缩放
setscale(strtoint(edit5.Text)/100,strtoint(edit5.Text)/100);
if i>0 then RemoveDepth:=true;
end;
gifSprite.ShowFrame(round(gif.Images[i].GraphicControlExtension.Delay/100*12)+1);
//延时
end;
Movie.PlaceObject(gifSprite, 3);//放在3层
EM := Movie.AddExternalMovie(edit2.Text, eimSprite, true);//载入原始SWF文件
With Movie.PlaceObject(EM.Sprite, 1) do//放在1层
begin
Name := 'd1';
end;
然后,添加多幅图片。本例要求SWF文件必须有透明部分,图片作为它的背景而存在,像QQ动态大头贴。将所用图片文件名放入listbox1中,图片扩展名可以是JPG、BMP、GIF、PNG(注意需要引入相应的单元文件),再按列表框中的顺序将图片加到平台上,设置相应的效果和延时。核心代码及注释如下:
for i := 0 to listbox1.Count-1 do//将列表框中每一幅图片加到库中
Movie.AddShapeImage(listbox1.Items[i]);
EM := Movie.AddExternalMovie(edit1.Text, eimSprite, true);//载入外部SWF,形成精灵。
Randomize;
// 根据效果生成动画(随机)
for i := 0 to listbox1.Count-1 do
begin
xg(Movie,em,i,random(6)+1,3);
Movie.RemoveObject(1);
end;
procedure tpswf.xg(xMovie: TFlashMovie;xEM: TFlashExternalMovie;nxh,nxg:integer;delaytime:integer);//影片,第几幅图,效果号,延时时间
var
i:integer;
mask: tflashshape;
tempmaskSprite,maskSprite: TFlashSprite;
begin
case nxg of
1: //显示
begin
xMovie.PlaceObject(xMovie.Shapes[nxh], 1);
xMovie.PlaceObject(xEM.Sprite, 3) ;//图片放在第一层,原SWF文件精灵放在第三层
xMovie.ShowFrame(myDelay(delaytime));
end;
2: //渐出
begin
for i:=myDelay(delaytime) downto 0 do
begin
with xMovie.PlaceObject(xMovie.Shapes[nxh], 1) do
begin
colortransform.addA:=-round(i/mydelay(delaytime)*$FF);//改变colortransform值实现渐显效果
if i<myDelay(delaytime) then RemoveDepth := true;
end;
xMovie.PlaceObject(xEM.Sprite, 3);
xMovie.ShowFrame;
end;
xMovie.ShowFrame(6);
end;
3://圆形
begin
mask := xMovie.AddCircle(round(swfw/2), round(swfh/2),round(sqrt(swfw*swfw+swfh*swfh)/2));//画圆
mask.SetSolidColor(cswfGreen);
maskSprite := xMovie.AddSprite;
maskSprite.PlaceObject(mask, 1);//设置遮罩
for i:=0 to myDelay(delaytime) do
begin
With maskSprite.PlaceObject(mask, 1) do
begin
translatex:= round(swfw/2-swfw/2/myDelay(delaytime)*i);
translatey:= round(swfh/2-swfh/2/myDelay(delaytime)*i);
SetScale(i/myDelay(delaytime), i/myDelay(delaytime));
RemoveDepth := true;
end;
maskSprite.ShowFrame;
end;
xMovie.PlaceObject(xMovie.Shapes[nxh],maskSprite, 1);
With xMovie.PlaceObject(xEM.Sprite, 3) do
begin
Name := 'd2';
end;
xMovie.ShowFrame(myDelay(delaytime));
end;
|