Android实例-MotionSensor加速度(XE8+小米2)

时间:2022-12-20 17:23:38

Android实例-MotionSensor加速度(XE8+小米2)

结果:

1.

实例代码:

 unit Unit1;

 interface

 uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Sensors,
FMX.StdCtrls, FMX.Controls.Presentation, System.Sensors.Components; type
TForm1 = class(TForm)
MotionSensor1: TMotionSensor;
Switch1: TSwitch;
Label1: TLabel;
Timer1: TTimer;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel;
Label15: TLabel;
Label16: TLabel;
Label17: TLabel;
procedure Switch1Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} procedure TForm1.FormCreate(Sender: TObject);
begin
Timer1.Enabled := False;
end; procedure TForm1.Switch1Click(Sender: TObject);
begin
MotionSensor1.Active := Switch1.IsChecked;
Timer1.Enabled := Switch1.IsChecked;
end; procedure TForm1.Timer1Timer(Sender: TObject);
var
LProp: TCustomMotionSensor.TProperty;//所有可能得到的参数
begin
for LProp in MotionSensor1.Sensor.AvailableProperties do//开始循环,如果手机支持该参数则显示出来
begin
case LProp of
TCustomMotionSensor.TProperty.AccelerationX:
begin
Label10.Visible := True;
Label10.Text := Format('Acceleration X: %6.2f', [MotionSensor1.Sensor.AccelerationX]);
end;
TCustomMotionSensor.TProperty.AccelerationY:
begin
Label11.Visible := True;
Label11.Text := Format('Acceleration Y: %6.2f', [MotionSensor1.Sensor.AccelerationY]);
end;
TCustomMotionSensor.TProperty.AccelerationZ:
begin
Label12.Visible := True;
Label12.Text := Format('Acceleration Z: %6.2f', [MotionSensor1.Sensor.AccelerationZ]);
end;
TCustomMotionSensor.TProperty.AngleAccelX:
begin
Label13.Visible := True;
Label13.Text := Format('Angle X: %6.2f', [MotionSensor1.Sensor.AngleAccelX]);
end;
TCustomMotionSensor.TProperty.AngleAccelY:
begin
Label14.Visible := True;
Label14.Text := Format('Angle Y: %6.2f', [MotionSensor1.Sensor.AngleAccelY]);
end;
TCustomMotionSensor.TProperty.AngleAccelZ:
begin
Label15.Visible := True;
Label15.Text := Format('Angle Z: %6.2f', [MotionSensor1.Sensor.AngleAccelZ]);
end;
TCustomMotionSensor.TProperty.Motion:
begin
Label16.Visible := True;
Label16.Text := Format('Motion: %6.2f', [MotionSensor1.Sensor.Motion]);
end;
TCustomMotionSensor.TProperty.Speed:
begin
Label17.Visible := True;
Label17.Text := Format('Speed: %6.2f', [MotionSensor1.Sensor.Speed]);
end;
end;
end;
end; end.