HarmonyOS 传感器判断屏幕方向?

通过传感器获取到OrientationResponse后,怎样根据OrientationResponse数据判断手机屏幕的方向?

阅读 502
1 个回答

调用OrientationResponse接口可以读取到alpha、beta、gamma三个属性分别表示Z轴、X轴、Y轴的旋转角度,可以读取角度进行判断,示例参考如下:

import sensor from "@ohos.sensor"
import BusinessError from "@ohos.base"

try {
  sensor.once(sensor.SensorId.ORIENTATION, (data: sensor.OrientationResponse) => {
    console.info('Succeeded in the device rotating at an angle around the X axis: ' + data.beta);
    console.info('Succeeded in the device rotating at an angle around the Y axis: ' + data.gamma);
    console.info('Succeeded in the device rotating at an angle around the Z axis: ' + data.alpha);
  });
} catch (error) {
  let e: BusinessError.BusinessError = error as BusinessError.BusinessError;
  console.error(`Failed to invoke once. Code: ${e.code}, message: ${e.message}`);
}
logo
HarmonyOS
子站问答
访问
宣传栏