2012-01-11 72 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