android加速计坐标,如何使用Android中的加速度计在XY平面中测量手机的倾斜度

加速计足以检查手机是否放平,正如Hoan很好地演示的那样。

对于到达这里的任何人,不仅要检查电话是否平整,还可以检查电话的旋转方向,这可以通过“ 旋转矢量运动传感器”实现。

private double pitch, tilt, azimuth;

@Override

public void onSensorChanged(SensorEvent event) {

//Get Rotation Vector Sensor Values

double[] g = convertFloatsToDoubles(event.values.clone());

//Normalise

double norm = Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2] + g[3] * g[3]);

g[0] /= norm;

g[1] /= norm;

g[2] /= norm;

g[3] /= norm;

//Set values to commonly known quaternion letter representatives

double x = g[0];

double y = g[1];

double z = g[2];

double w = g[3];

//Calculate Pitch in degrees (-180 to 180)

double sinP = 2.0 * (w * x + y * z);

double cosP = 1.0 - 2.0 * (x * x + y * y);

pitch = Math.atan2(sinP, cosP) * (180 / Math.PI);

//Calculate Tilt in degrees (-90 to 90)

double sinT = 2.0 * (w * y - z * x);

if (Math.abs(sinT) >= 1)

tilt = Math.copySign(Math.PI / 2, sinT) * (180 / Math.PI);

else

tilt = Math.asin(sinT) * (180 / Math.PI);

//Calculate Azimuth in degrees (0 to 360; 0 = North, 90 = East, 180 = South, 270 = West)

double sinA = 2.0 * (w * z + x * y);

double cosA = 1.0 - 2.0 * (y * y + z * z);

azimuth = Math.atan2(sinA, cosA) * (180 / Math.PI);

}

private double[] convertFloatsToDoubles(float[] input)

{

if (input == null)

return null;

double[] output = new double[input.length];

for (int i = 0; i < input.length; i++)

output[i] = input[i];

return output;

}

然后,要检查手机是否平坦,您可以简单地将tilt和pitch值与容差值进行比较。例如

public boolean flatEnough(double degreeTolerance) {

return tilt <= degreeTolerance && tilt >= -degreeTolerance && pitch <= degreeTolerance && pitch >= -degreeTolerance;

}

这样做的好处是您可以检查手机是否旋转了特定角度。

值得注意的是,应用程序的方向不会影响俯仰,倾斜和方位角的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值