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

结果:

1.

实例代码:

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, System.Sensors,
  8   FMX.StdCtrls, FMX.Controls.Presentation, System.Sensors.Components;
  9 
 10 type
 11   TForm1 = class(TForm)
 12     MotionSensor1: TMotionSensor;
 13     Switch1: TSwitch;
 14     Label1: TLabel;
 15     Timer1: TTimer;
 16     Label2: TLabel;
 17     Label3: TLabel;
 18     Label4: TLabel;
 19     Label5: TLabel;
 20     Label6: TLabel;
 21     Label7: TLabel;
 22     Label8: TLabel;
 23     Label9: TLabel;
 24     Label10: TLabel;
 25     Label11: TLabel;
 26     Label12: TLabel;
 27     Label13: TLabel;
 28     Label14: TLabel;
 29     Label15: TLabel;
 30     Label16: TLabel;
 31     Label17: TLabel;
 32     procedure Switch1Click(Sender: TObject);
 33     procedure Timer1Timer(Sender: TObject);
 34     procedure FormCreate(Sender: TObject);
 35   private
 36     { Private declarations }
 37   public
 38     { Public declarations }
 39   end;
 40 
 41 var
 42   Form1: TForm1;
 43 
 44 implementation
 45 
 46 {$R *.fmx}
 47 {$R *.NmXhdpiPh.fmx ANDROID}
 48 
 49 procedure TForm1.FormCreate(Sender: TObject);
 50 begin
 51   Timer1.Enabled := False;
 52 end;
 53 
 54 procedure TForm1.Switch1Click(Sender: TObject);
 55 begin
 56   MotionSensor1.Active := Switch1.IsChecked;
 57   Timer1.Enabled := Switch1.IsChecked;
 58 end;
 59 
 60 procedure TForm1.Timer1Timer(Sender: TObject);
 61 var
 62   LProp: TCustomMotionSensor.TProperty;//所有可能得到的参数
 63 begin
 64   for LProp in MotionSensor1.Sensor.AvailableProperties do//开始循环,如果手机支持该参数则显示出来
 65   begin
 66     case LProp of
 67       TCustomMotionSensor.TProperty.AccelerationX:
 68       begin
 69         Label10.Visible := True;
 70         Label10.Text := Format('Acceleration X: %6.2f', [MotionSensor1.Sensor.AccelerationX]);
 71       end;
 72       TCustomMotionSensor.TProperty.AccelerationY:
 73       begin
 74         Label11.Visible := True;
 75         Label11.Text := Format('Acceleration Y: %6.2f', [MotionSensor1.Sensor.AccelerationY]);
 76       end;
 77       TCustomMotionSensor.TProperty.AccelerationZ:
 78       begin
 79         Label12.Visible := True;
 80         Label12.Text := Format('Acceleration Z: %6.2f', [MotionSensor1.Sensor.AccelerationZ]);
 81       end;
 82       TCustomMotionSensor.TProperty.AngleAccelX:
 83       begin
 84         Label13.Visible := True;
 85         Label13.Text := Format('Angle X: %6.2f', [MotionSensor1.Sensor.AngleAccelX]);
 86       end;
 87       TCustomMotionSensor.TProperty.AngleAccelY:
 88       begin
 89         Label14.Visible := True;
 90         Label14.Text := Format('Angle Y: %6.2f', [MotionSensor1.Sensor.AngleAccelY]);
 91       end;
 92       TCustomMotionSensor.TProperty.AngleAccelZ:
 93       begin
 94         Label15.Visible := True;
 95         Label15.Text := Format('Angle Z: %6.2f', [MotionSensor1.Sensor.AngleAccelZ]);
 96       end;
 97       TCustomMotionSensor.TProperty.Motion:
 98       begin
 99         Label16.Visible := True;
100         Label16.Text := Format('Motion: %6.2f', [MotionSensor1.Sensor.Motion]);
101       end;
102       TCustomMotionSensor.TProperty.Speed:
103       begin
104         Label17.Visible := True;
105         Label17.Text := Format('Speed: %6.2f', [MotionSensor1.Sensor.Speed]);
106       end;
107     end;
108   end;
109 end;
110 
111 end.
原文地址:https://www.cnblogs.com/FKdelphi/p/4789772.html