2012-01-11 69 views
1

自Android 3.0以来,必须重新设置android.hardware.SensorManager.getRotationMatrix方法的结果才能像之前的Android版本一样工作。Android加速度计和Android版本

我用下面的代码:

android.hardware.SensorManager.getRotationMatrix(newm, null, mAccelerometerValues, mMagneticValues); 
if(sdk >= 11){ //Honeycomb 
      android.hardware.SensorManager.remapCoordinateSystem(newm, 
                  android.hardware.SensorManager.AXIS_MINUS_Y, 
                  android.hardware.SensorManager.AXIS_X, 
                  newm); 
     } 

现在,在Android冰淇淋三明治(4.0)中的SensorManager作为在Android 2.x的 我的问题是:

加速计只能在平板电脑上旋转?加速计仅在Android 3.X上旋转?如果有任何示例说明如何在任何Android版本中读取加速计?

回答

3

经过一番调查中,我找到了解决办法:

片中有景观为默认方向,所以屏幕旋转为0或180时的方向是横向,和智能手机有肖像。我用下面的代码到平板电脑和智能手机之间的区别:

Display display = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
    int orientation = getScreenOrientation(display); 
    int rotation = display.getRotation(); 

    remapCoordinates = (orientation == Configuration.ORIENTATION_LANDSCAPE && rotation == Surface.ROTATION_0) || 
      (orientation == Configuration.ORIENTATION_LANDSCAPE && rotation == Surface.ROTATION_180) || 
      (orientation == Configuration.ORIENTATION_PORTRAIT && rotation == Surface.ROTATION_90) || 
      (orientation == Configuration.ORIENTATION_PORTRAIT && rotation == Surface.ROTATION_270); 

其中getScreenOrientation方法是:

public static int getScreenOrientation(Display display){ 


    int orientation; 

    if(display.getWidth()==display.getHeight()){ 
       orientation = Configuration.ORIENTATION_SQUARE; 
    }else{ //if widht is less than height than it is portrait 
       if(display.getWidth() < display.getHeight()){ 
       orientation = Configuration.ORIENTATION_PORTRAIT; 
       }else{ // if it is not any of the above it will defineitly be landscape 
       orientation = Configuration.ORIENTATION_LANDSCAPE; 
       } 
    } 
    return orientation; 
} 

现在,我重新映射坐标只有当默认的方向是横向前:

if(remapCoordinates){ 
      android.hardware.SensorManager.remapCoordinateSystem(newm, 
                  android.hardware.SensorManager.AXIS_MINUS_Y, 
                  android.hardware.SensorManager.AXIS_X, 
                  newm); 
} 

此致敬礼。

+0

我在哪里得到这个newm变量在你最后的Codeblock?谢谢 – vacetahanna 2013-01-30 13:59:04