[iOS, Android] Как масштабировать изображение пальцами?
// Способ первый
// Изменяя размеры компонента
var
FLastDistance: Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.Touch.InteractiveGestures := [TInteractiveGesture.Zoom];
end;
procedure TForm1.Image1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
var
LObj: IControl;
LImage: TImage;
LImageCenter: TPointF;
begin
if EventInfo.GestureID = igiZoom then
begin
LObj := Self.ObjectAtPoint(ClientToScreen(EventInfo.Location));
if LObj is TImage then
begin
if (not(TInteractiveGestureFlag.gfBegin in EventInfo.Flags)) and
(not(TInteractiveGestureFlag.gfEnd in EventInfo.Flags)) then
begin
{ zoom the image }
LImage := TImage(LObj.GetObject);
LImageCenter := LImage.Position.Point + PointF(LImage.Width / 2,
LImage.Height / 2);
LImage.Width := LImage.Width + (EventInfo.Distance - FLastDistance);
LImage.Height := LImage.Height + (EventInfo.Distance - FLastDistance);
LImage.Position.X := LImageCenter.X - LImage.Width / 2;
LImage.Position.Y := LImageCenter.Y - LImage.Height / 2;
end;
FLastDistance := EventInfo.Distance;
end;
end;
end;
// Способ второй
// Изменяя коэффициент масштабирования
var
FLastDistance: Integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Image1.Touch.InteractiveGestures := [TInteractiveGesture.Zoom];
end;
procedure TForm1.Image1Gesture(Sender: TObject;
const EventInfo: TGestureEventInfo; var Handled: Boolean);
var
LObj: IControl;
LImage: TImage;
LImageCenter: TPointF;
ScaleFactorX, ScaleFactorY: Single;
begin
if EventInfo.GestureID = igiZoom then
begin
LObj := Self.ObjectAtPoint( ClientToScreen( EventInfo.Location ) );
if LObj is TImage then
begin
if ( not( TInteractiveGestureFlag.gfBegin in EventInfo.Flags ) ) and
( not( TInteractiveGestureFlag.gfEnd in EventInfo.Flags ) ) then
begin
{ zoom the image }
LImage := TImage(LObj.GetObject);
LImageCenter := LImage.Position.Point + PointF((LImage.Width * LImage.Scale.X) / 2,
(LImage.Height * LImage.Scale.Y) / 2);
ScaleFactorX := LImage.Scale.X + ((EventInfo.Distance - FLastDistance) / 300);
ScaleFactorY := LImage.Scale.Y + ((EventInfo.Distance - FLastDistance) / 300);
LImage.Scale.X := ScaleFactorX;
LImage.Scale.Y := ScaleFactorY;
LImage.Position.X := LImageCenter.X - (LImage.Width * ScaleFactorX) / 2;
LImage.Position.Y := LImageCenter.Y - (LImage.Height * ScaleFactorY) / 2;
end;
FLastDistance := EventInfo.Distance;
end;
end;
end;
|