方向用于设置Camera.Parameters的旋转.
我使用SensorManager.getRotationMatrix()来计算方向,但它需要来自地磁传感器的值.
联想S90-A没有指南针,所以似乎没有办法让我获得这些价值.
我试着使用这段代码:
int rotation = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultdisplay().getRotation();
从我的服务,但它只在设备打开时有效.
但是如果设备正在休眠并且我收到推送通知,则此方法始终返回Surface.ROTATION_0.
设备将固定在墙上,不应移动.
那么,有没有办法在没有罗盘的情况下检测当前的设备方向?
解决方法 这个 answer非常有帮助.实际上,如果您的设备的方向是平的(例如,它位于桌子上),您不能仅依靠加速度计.我最终得到了这种方法,我用于没有指南针的设备.
对于带指南针的设备,我使用SensorManager.getRotationMatrix()和SensorManager.getorIEntation().
/** * calculates rotation only from accelerometer values * @param g - accelerometer event values * @return */ private int getRotationFromAccelerometerOnly(float[] g) { double normOfG = Math.sqrt(g[0] * g[0] + g[1] * g[1] + g[2] * g[2]); // normalize the accelerometer vector g[0] = (float) (g[0] / normOfG); g[1] = (float) (g[1] / normOfG); g[2] = (float) (g[2] / normOfG); int inclination = (int) Math.round(Math.todegrees(Math.acos(g[2]))); int rotation; if (inclination < 25 || inclination > 155) { // device is flat,return 0 rotation = 0; } else { // device is not flat rotation = (int) Math.round(Math.todegrees(Math.atan2(g[0],g[1]))); } return rotation; }总结
以上是内存溢出为你收集整理的android – 在没有指南针的设备上获取服务方向全部内容,希望文章能够帮你解决android – 在没有指南针的设备上获取服务方向所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)