Android实例-GestureManager手势支持放大缩小左右移动上下移动

 

 

相关资料:

https://download.csdn.net/download/zhujianqiangqq/62483987         代码包下载

 

实例代表:

Unit1.fmx

界面中有三个图片,太大不方便上传,请从CSDN上下载代码包。

Unit1.pas

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  8   FMX.Controls.Presentation, FMX.StdCtrls, FMX.Gestures, FMX.TabControl,
  9   FMX.Objects
 10   ,System.Generics.Collections
 11   ,System.Math, FMX.Layouts
 12   ;
 13 
 14 type
 15   TForm1 = class(TForm)
 16     GestureManager1: TGestureManager;
 17     Image1: TImage;
 18     Rectangle1: TRectangle;
 19     Layout1: TLayout;
 20     Image2: TImage;
 21     Layout2: TLayout;
 22     Image3: TImage;
 23     Image4: TImage;
 24     Label1: TLabel;
 25     procedure FormGesture(Sender: TObject; const EventInfo: TGestureEventInfo;
 26       var Handled: Boolean);
 27     procedure Image2Click(Sender: TObject);
 28   private
 29    { Private declarations }
 30     FLastPosition       : TPointF;
 31     FLastDIstance       : integer ;
 32     FOriginalWidth      : Single ;
 33     LImage: TImage;
 34   public
 35     { Public declarations }
 36   end;
 37 
 38 var
 39   Form1: TForm1;
 40 
 41 implementation
 42 
 43 {$R *.fmx}
 44 
 45 procedure TForm1.FormGesture(Sender: TObject;
 46   const EventInfo: TGestureEventInfo; var Handled: Boolean);
 47 var
 48   LObj: IControl;
 49   LImageCenter: TPointF;
 50   Tan :single;
 51   A,B :single;
 52 begin
 53   inherited;
 54 
 55   Label1.Text := IntToStr(EventInfo.GestureID);
 56   if EventInfo.GestureID = igiZoom then//放大缩小手势
 57   begin
 58     LObj := Self.ObjectAtPoint(ClientToScreen(EventInfo.Location));
 59     if LObj is TImage then
 60     begin
 61       if (not(TInteractiveGestureFlag.gfBegin in EventInfo.Flags)) and
 62         (not(TInteractiveGestureFlag.gfEnd in EventInfo.Flags)) then
 63       begin
 64         LImage := TImage(LObj.GetObject);
 65         LImageCenter := LImage.Position.Point + PointF(LImage.Width / 2, LImage.Height / 2);
 66         if LImage.Width<FOriginalWidth then
 67         begin
 68           LImage.Width := LImage.Width +  (power(2,(EventInfo.Distance - FLastDistance)/10)-1)*10; // 控制u图片缩小速度和大小,滑的越大变化越小,后面变化无限小
 69           LImage.Height := LImage.Height + (power(2,(EventInfo.Distance - FLastDistance)/10)-1)*10;//
 70         end
 71         else
 72         begin
 73           LImage.Width := LImage.Width +  2*(EventInfo.Distance - FLastDistance);
 74           LImage.Height := LImage.Height + 2*(EventInfo.Distance - FLastDistance);
 75         end;
 76         LImage.Position.X := LImageCenter.X - LImage.Width / 2;
 77         LImage.Position.Y := LImageCenter.Y - LImage.Height / 2;
 78       end;
 79       FLastDistance := EventInfo.Distance;
 80     end;
 81     Exit;
 82   end;
 83 
 84   //平移
 85   if EventInfo.GestureID = igiPan then
 86   begin
 87     LObj := Self.ObjectAtPoint(ClientToScreen(EventInfo.Location));
 88     if LObj is TImage then
 89     begin
 90       if not(TInteractiveGestureFlag.gfBegin in EventInfo.Flags) then
 91       begin
 92         LImage := TImage(LObj.GetObject);
 93         if (LImage <> nil) then
 94         begin
 95           LImage.Position.X := LImage.Position.X + (EventInfo.Location.X - FLastPosition.X);
 96           LImage.Position.Y := LImage.Position.Y + (EventInfo.Location.Y - FLastPosition.Y);
 97         end;
 98       end;
 99       FLastPosition := EventInfo.Location;
100       Exit;
101     end;
102   end;
103 end;
104 
105 procedure TForm1.Image2Click(Sender: TObject);
106 var
107   oImage: TImage;
108 begin
109   if Sender is TImage then
110   begin
111     oImage := (Sender as TImage);
112     Image1.Bitmap.Assign(oImage.Bitmap);
113   end;
114 end;
115 
116 end.
View Code

 

posted on 2021-12-13 18:41  疯狂delphi  阅读(164)  评论(0编辑  收藏  举报

导航