2012-02-14 24 views
0

中获取手机的位置正在尝试了解如何知道该设备的屏幕是面向用户的,或者,例如,平板上的屏幕抬头或面朝下等。在android

我没有使用方向传感器,因为它已过时我使用旋转矩阵,我知道音调和滚动值应该是很好的信息,但在获得这些值之前,我需要重新协调系统,并且知道哪个Axle是X,哪个是YI需要知道屏幕正在面对什么。

例如:如果屏幕朝向用户I可以使用此映射表

  ROTATION_0       X  ý

  ROTATION_90 -Y       X

  ROTATION_180 -X   -Y

ROTATION_270  Ÿ  -X

如果屏幕对着天空也许我会需要更换Y轴均与Z轰,但是我不知道我怎么能找出是对着屏幕。

任何想法?

我也使用了我在网上找到的这种方法(http://code.google.com/p/the-schwartz-unsheathed/source/browse/trunk/src/com/android/app /schwarz/PhoneOrientation.java?r=12)

public static int getOrientation(float roll, float pitch) { 
       int orientation = ORIENTATION_INVALID; 

       if (Math.abs(roll) == 0.0 && Math.abs(pitch) == 0.0){ 
        return ORIENTATION_INVALID; 
       } 

       if(roll >= -90 && roll <= -(90-mTolerance)){ 
         orientation = ORIENTATION_FACE_LEFT; 
       } 
       else if(roll <= 90 && roll >= (90-mTolerance)){ 
         orientation = ORIENTATION_FACE_RIGHT; 
        } 

       if(pitch >= (90-mTolerance) && pitch <= (90+mTolerance)) { 
         if(orientation != ORIENTATION_INVALID){ 
           orientation = ORIENTATION_INVALID; 
         } 
         else { 
           orientation = ORIENTATION_FACE_FORWARD; 
         } 
       } else if(pitch <= -(90-mTolerance) && pitch >= -(90+mTolerance)) { 
         if(orientation != ORIENTATION_INVALID) { 
           orientation = ORIENTATION_INVALID; 
         } 
         else { 
           orientation = ORIENTATION_FACE_BACKWARD; 
         } 
       } 

       if((roll >= -mTolerance && roll <= mTolerance)) { 
         if((pitch >= -mTolerance && pitch <= mTolerance)) { 
           if(orientation != ORIENTATION_INVALID) { 
             orientation = ORIENTATION_INVALID; 
           } 
           else { 
             orientation = ORIENTATION_FACE_UP; 
           } 
         } else if((pitch <= -(180-mTolerance) && pitch >= -180) || (pitch >= (180-mTolerance) && pitch <= 180)) { 
           if(orientation != ORIENTATION_INVALID) { 
             orientation = ORIENTATION_INVALID; 
           } 
           else { 
             orientation = ORIENTATION_FACE_DOWN; 
           } 
         } 
       } 

       return orientation; 
     } 

任何人都有一个更好?

+0

如果某人正在翻阅屏幕 - 例如当躺在床上?那么这个解决方案根本不起作用,对吧? 你也可以做脸部识别,虽然它有点矫枉过正我猜... – 2013-12-13 14:57:10

回答

0

您可以使用加速度计,因为它给出设备的x-y轴,因此您可以获取设备所面对的位置。

+0

这是不正确的,它只是正确的,如果屏幕面对用户,如果屏幕面对天空是不同的。屏幕坐标系不变,但传感器坐标系确实如此。这就是我们应该使用remapCoordinateSystem的原因。 – polonskyg 2012-02-14 12:13:26