如何使用Android传感器事件,以确定设备朝上或朝下 [英] How to use Android sensor event to determine if device is facing up or down

查看:135
本文介绍了如何使用Android传感器事件,以确定设备朝上或朝下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的要求。假设设备是站在其端,垂直于地面,而它是倾斜的,所有我需要确定是手机是否被向前或向后倾斜(屏幕更朝向地面或者更朝向天花板)。

我知道如何从各种传感器读取值和我的身影,使用传感器TYPE_ROTATION_VECTOR是前进的方向。所有我缺少的是数学知识来确定从它返回三个值向前或向后。

我读过的所有相关的线程上因此,没有启蒙,任何帮助非常AP preciated。


解决方案

 浮动[] = rotationMatrix新的浮动[9];
浮动[] = inclinationMatrix新的浮动[9];
浮动[]加速度计;从传感器//值
浮动[]磁性;从传感器//值
SensorManager.getRotationMatrix(rotationMatrix,inclinationMatrix,加速计,磁力)
INT倾角=(int)的Math.round(Math.toDegrees(Math.acos(rotationMatrix [8])));如果(倾角和LT; 90)
{
      //面对
}如果(倾斜大于90)
{
       // 面朝下
}

I have quite a simple requirement. Assuming a device is standing on its end, perpendicular to the ground, and it is tilted, all I need to determine is whether the phone is tilted forward or back (screen more toward the ground or more toward the ceiling).

I know how to read values from the various sensors and I figure that using sensor TYPE_ROTATION_VECTOR is the way forward. All I'm missing is the maths know-how to determine forward or back from the three values it returns.

I've read all related threads on SO without enlightenment, any help very much appreciated.

解决方案

float[] rotationMatrix = new float[9];
float[] inclinationMatrix = new float[9];
float[] accelerometer; // values from sensor
float[] magnetic; // values from sensor
SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magnetic) 
int inclination = (int) Math.round(Math.toDegrees(Math.acos(rotationMatrix[8])));

if (inclination < 90)
{
      // face up
}

if (inclination > 90)
{
       // face down
}

这篇关于如何使用Android传感器事件,以确定设备朝上或朝下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆