Delphi 在FireMonkey应用程序中使用TOrientationSensor获取设备倾斜和指南针航向


本文整理自网络,侵删。

 
当TOrientationSensor组件的Active属性设置为True时,将获取信息。

OrientationSensor1.Active := True;
在OnSensorChoosing事件中,指定要使用的传感器。

以下代码检查并选择可以获取设备倾斜度的传感器。

procedure TForm1.OrientationSensor1SensorChoosing(Sender: TObject;
  const Sensors: TSensorArray; var ChoseSensorIndex: Integer);
var
  I: Integer;
begin
  for I := 0 to High(Sensors) do
  begin
    if TCustomOrientationSensor.TProperty.TiltX
      in TCustomOrientationSensor(Sensors[I]).AvailableProperties then
    begin
      ChoseSensorIndex := I;
      Exit;
    end;
  end;
  ChoseSensorIndex := 0;
end;
以下代码检查并选择可以获取指南针航向的传感器。

procedure TForm1.OrientationSensor2SensorChoosing(Sender: TObject;
  const Sensors: TSensorArray; var ChoseSensorIndex: Integer);
var
  I: Integer;
begin
  for I := 0 to High(Sensors) do
  begin
    if TCustomOrientationSensor.TProperty.HeadingX
      in TCustomOrientationSensor(Sensors[I]).AvailableProperties then
    begin
      ChoseSensorIndex := I;
      Exit;
    end;
  end;
  ChoseSensorIndex := 0;
end;
在OnDataChanged事件中,获取传感器信息。

以下代码显示设备倾斜度。

procedure TForm1.OrientationSensor1DataChanged(Sender: TObject);
begin
  LabelTiltX.Text := FloatToStr(OrientationSensor1.Sensor.TiltX);
  LabelTiltY.Text := FloatToStr(OrientationSensor1.Sensor.TiltY);
  LabelTiltZ.Text := FloatToStr(OrientationSensor1.Sensor.TiltZ);
end;
以下代码显示指南针的方向。

procedure TForm1.OrientationSensor2DataChanged(Sender: TObject);
begin
  LabelHeadingX.Text := FloatToStr(OrientationSensor2.Sensor.HeadingX);
  LabelHeadingY.Text := FloatToStr(OrientationSensor2.Sensor.HeadingY);
  LabelHeadingZ.Text := FloatToStr(OrientationSensor2.Sensor.HeadingZ);
end;

相关阅读 >>

tstringlist 常用操作

Delphi 错误no ftp list parsers have been registered

提供文件操作 单元

Delphi 如何从html格式的字符串中提取纯文本?

Delphi firemonkey的屏幕分辨率hdpi、mdpi、ldpi的差别

Delphi在64位系统下读写注册表

Delphi xe2读取内存偏移数据代码

Delphi xe10 手机端错误提示:detected problems with api compatibility (visit g.co/dev/appcompat for more info

Delphi的rtti实现对象的xml持久化

Delphi image1 图像旋转90

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...