首页 > 解决方案 > Delphi中的旋转图像

问题描述

我正在测试如何在 Delphi 中旋转图像(在 Android 上)。出于某种原因,它只有在我在屏幕上移动两根手指时才有效。而且旋转不顺畅。理想情况下,用一根手指点击图像我想让图像旋转,直到它被另一次点击停止。另外,有没有更好的更像德尔福的方法来实现这一点?我有这个代码(RAD Delphi 10.4):

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
  FMX.Colors, System.IOUtils, FMX.Gestures, System.Math, FMX.Media;

type
  TForm1 = class(TForm)
    ColorBox1: TColorBox;
    ColorBox2: TColorBox;
    ColorBox3: TColorBox;
    ColorBox4: TColorBox;
    ColorBox5: TColorBox;
    ColorBox6: TColorBox;
    Image1: TImage;
    GestureManager1: TGestureManager;
    MediaPlayer1: TMediaPlayer;
    procedure ColorBox1Click(Sender: TObject);
    procedure ColorBox2Click(Sender: TObject);
    procedure ColorBox3Click(Sender: TObject);
    procedure ColorBox4Click(Sender: TObject);
    procedure ColorBox5Click(Sender: TObject);
    procedure ColorBox6Click(Sender: TObject);
    procedure Image1Gesture(Sender: TObject; const EventInfo: TGestureEventInfo;
      var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}
{$R *.LgXhdpiPh.fmx ANDROID}

procedure TForm1.ColorBox1Click(Sender: TObject);
var
  number: Integer;
  stop: Boolean;

begin
  //Image1.Bitmap.LoadFromFile('../../images/black.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'black.png');
  MediaPlayer1.FileName := TPath.Combine(TPath.GetDocumentsPath, 'spinner.3gp');
  MediaPlayer1.Play;
end;

procedure TForm1.ColorBox2Click(Sender: TObject);
begin
  //Image1.Bitmap.LoadFromFile('../../images/blue.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'blue.png');
end;

procedure TForm1.ColorBox3Click(Sender: TObject);
begin
  //Image1.Bitmap.LoadFromFile('../../images/red.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'red.png');
end;

procedure TForm1.ColorBox4Click(Sender: TObject);
begin
  //Image1.Bitmap.LoadFromFile('../../images/green.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'green.png');
end;

procedure TForm1.ColorBox5Click(Sender: TObject);
begin
  //Image1.Bitmap.LoadFromFile('../../images/yellow.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'yellow.png');
end;

procedure TForm1.ColorBox6Click(Sender: TObject);
begin
  //Image1.Bitmap.LoadFromFile('../../images/pink.png')
  Image1.Bitmap.LoadFromFile(TPath.GetDocumentsPath + PathDelim + 'pink.png');
end;

procedure TForm1.Image1Gesture(Sender: TObject;
  const EventInfo: TGestureEventInfo; var Handled: Boolean);
  var
    LObj: IControl;
    image: TImage;
begin
  LObj := Self.ObjectAtPoint(ClientToScreen(EventInfo.Location));
  if LObj is TImage then
  begin
    image := TImage(LObj.GetObject);
    image.RotationAngle := RadToDeg(-EventInfo.Angle);
  end;
end;

end.

标签: delphibitmap

解决方案


我想最好使用 TImage 的 OnClick 事件而不是 Gesture。

const
  RotationDelta = 0.5;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  Timer1.Enabled := false; //to disable rotation
  Timer1.Interval := 20;
end;

procedure TForm1.Image1Click(Sender: TObject);
begin
   Timer1.Enabled := not Timer1.Enabled; //Timer.Interval should be 20-30 ms
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Image1.RotationAngle := Image1.RotationAngle + RotationDelta; //rotate image
end;

这不是很好的实现,因为 TTimer 不是很准确,但对于一般用途来说已经足够了。如果您想要更慢或更快的旋转,您应该分别更改 RotationDelta。

但是我的建议只有在您想通过单击图像来启用/禁用旋转时才有效,而不是在滑动时。

PS 在 Delphi 10.1 上检查了这个解决方案,但仅在 Windows 上。


推荐阅读