在 Delphi FireMonkey 框架中,TSwipeTransitionEffect
是一个强大的过渡效果组件,可以用于 模拟翻页、滑动切换图片 等动画效果。本文将介绍如何使用 TSwipeTransitionEffect
结合 TPathAnimation
来创建流畅的滑动动画。
资源文件:C:\Program Files (x86)\Embarcadero\Studio\22.0\Demos\Object Pascal\Multi-Device Samples\User Interface\FireFlow\Demo Photos
工程文件路径:“C:\delphicode\showimageview\Project1.dproj”
运行结果:
1. 什么是 TSwipeTransitionEffect
?
TSwipeTransitionEffect
允许你在 两个纹理 之间创建滑动过渡效果。它可以用于 图片切换、界面过渡,甚至 模拟书页翻转。
核心属性:
Target
:指定过渡的目标图片。Back
:定义过渡的背景图片。MousePoint
:控制滑动的起始位置。Deep
:调整滑动的折叠程度。
2. 创建滑动动画
步骤 1:添加 UI 组件
- 在 11.3 中创建一个 FireMonkey HD 应用。
- 在 Form 上添加:
TImage
(用于显示图片)。TSwipeTransitionEffect
(作为TImage
的子控件)。- selectionpoint1
TPathAnimation
(用于控制滑动路径)。- adoconnection
- adotable
- image1
- image2
- panel1
- speedbutton1
- speedbutton2
步骤 2:加载图片
try
Conn.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' + 'Data Source=C:\Users\86182\Desktop\图片信息统计.xls;' + 'Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";';
Conn.LoginPrompt := false;
Conn.Connected := true;
ADOTabXLS.Connection := Conn;
ADOTabXLS.TableName := '[' + 'Sheet' + '$]';
ADOTabXLS.Active := true;
except
;
end;
// Image1.Bitmap.LoadFromFile('C:\Program Files (x86)\Embarcadero\Studio\22.0\Demos\Object Pascal\Multi-Device Samples\User Interface\FireFlow\Demo Photos\Picture10.jpg');
Image1.Bitmap.LoadFromFile(ADOTabXLS.FieldByName('Path').AsString);
步骤 3:实现滑动动画
procedure TForm2.PathAnimation1Finish(Sender: TObject);
var
BitMap: TBitmap;
begin
BitMap := TBitmap.Create(0, 0);
BitMap.Assign(SwipeTransitionEffect1.Target);
SwipeTransitionEffect1.Target := Image1.Bitmap;
Image1.Bitmap.Assign(BitMap);
SwipeTransitionEffect1.MousePoint := PointF(0, 0);
end;
procedure TForm2.PathAnimation1Process(Sender: TObject);
begin
SwipeTransitionEffect1.MousePoint := SelectionPoint1.Position.Point;
end;
步骤 4:切换照片事件
procedure TForm2.Image1Click(Sender: TObject);
begin
if ADOTabXLS.Eof and ADOTabXLS.Bof then
begin
Exit;
end;
self.SwipeTransitionEffect1.Back := self.Image2.Bitmap;
ADOTabXLS.Prior;
Image2.Bitmap.LoadFromFile(ADOTabXLS.FieldByName('Path').AsString);
self.SwipeTransitionEffect1.Target := self.Image2.Bitmap;
PathAnimation1.Enabled := False;
SelectionPoint1.Position.Point := PointF(0, 0);
SelectionPoint1.Opacity := 0;
PathAnimation1.Parent := SelectionPoint1;
PathAnimation1.Path.Clear;
// begin Path for mouse pointer
PathAnimation1.Path.MoveTo(PointF(0, 0));
PathAnimation1.Path.LineTo(PointF(Form2.Width / 2, Form2.Height / 2));
PathAnimation1.Path.LineTo(PointF(Form2.Width * 2, 0));
PathAnimation1.Duration := 2;
PathAnimation1.Start;
end;
procedure TForm2.SpeedButton2Click(Sender: TObject);
begin
if ADOTabXLS.Eof and ADOTabXLS.Bof then
begin
Exit;
end;
self.SwipeTransitionEffect1.Back := self.Image1.Bitmap;
ADOTabXLS.Next;
;
Image2.Bitmap.LoadFromFile(ADOTabXLS.FieldByName('Path').AsString);
self.SwipeTransitionEffect1.Target := self.Image2.Bitmap;
PathAnimation1.Enabled := False;
SelectionPoint1.Position.Point := PointF(0, 0);
SelectionPoint1.Opacity := 0;
PathAnimation1.Parent := SelectionPoint1;
PathAnimation1.Path.Clear;
// begin Path for mouse pointer
PathAnimation1.Path.MoveTo(PointF(0, 0));
PathAnimation1.Path.LineTo(PointF(Form2.Width / 2, Form2.Height / 2));
PathAnimation1.Path.LineTo(PointF(Form2.Width * 2, 0));
PathAnimation1.Duration := 0.3;
PathAnimation1.Start;
end;
3. 运行效果
当你点击 图片 时,它会 滑动切换 到下一张图片,并模拟 翻页效果。你可以调整 Deep
和 MousePoint
以优化动画效果。