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事件,而不是手势。

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。

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

p.S.在Delphi 10.1上检查了此解决方案,但仅在Windows上检查。

相关内容

  • 没有找到相关文章

最新更新