【问题标题】:How to use Android sensor event to determine if device is facing up or down如何使用 Android 传感器事件来确定设备是朝上还是朝下
【发布时间】:2013-01-23 10:13:22
【问题描述】:

我有一个非常简单的要求。假设一个设备端立,垂直于地面,并且它是倾斜的,我只需要确定手机是向前还是向后倾斜(屏幕更靠近地面还是更靠近天花板)。

我知道如何从各种传感器读取值,并且我认为使用传感器 TYPE_ROTATION_VECTOR 是前进的方向。我所缺少的只是从它返回的三个值中确定向前或向后的数学知识。

我在没有任何启发的情况下阅读了所有关于 SO 的相关主题,非常感谢任何帮助。

【问题讨论】:

    标签: android rotation sensors


    【解决方案1】:
    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
    }
    

    【讨论】:

      【解决方案2】:

      X 轴水平指向右侧,Y 轴垂直指向上方,Z 轴指向屏幕正面外侧。在这个系统中,屏幕后面的坐标具有负 Z 值。

      参考坐标系定义为直接正交基,其中:

      X is defined as the vector product Y.Z (It is tangential to the ground at the device's current location and roughly points East).
      Y is tangential to the ground at the device's current location and points towards magnetic north.
      Z points towards the sky and is perpendicular to the ground.
      

      在你的情况下试试这个,

      if(Round(y,4) < 8.0){
                 Log.d("sensor", "=====UP====");
      
      
              }
      
              else if(Round(y,4) < -8.0){
                  Log.d("sensor", "=====DOWN====");
      
              }
      

      【讨论】:

      • 感谢您的快速回复。冒着听起来比我现在更愚蠢的风险,你的 Round() 函数指的是什么?
      【解决方案3】:

      您可以使用加速度计和磁场传感器来检测这一点。

      我发现这篇有用的博客文章有必要的代码来告诉你如何做到这一点。

      http://www.ahotbrew.com/how-to-detect-forward-and-backward-tilt/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-23
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        相关资源
        最近更新 更多