在Delphi中旋转图像

比鲁斯

我正在测试如何在Delphi中旋转图像(在Android上)。由于某些原因,只有当我在屏幕上移动两个手指时,它才起作用。而且旋转不平滑。理想情况下,我要用一根手指在图像上进行爬升,直到图像被另一次单击停止为止。另外,是否有更好的类似Delphi的方式来实现这一目标?我有以下代码(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.
无人

我想最好使用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上。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章