2012-01-11 5 views
1

Android 3.0以来、android.hardware.SensorManager.getRotationMatrixメソッドの結果は以前のAndroidバージョンと同様に動作するように再マップする必要があります。 Androidのアイスクリームサンドイッチ(4.0)SensorManagerは、Android 2.Xでも上として動作する上で、今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 3.Xでのみ回転しますか?どのAndroidのバージョンでも加速度計を読み取る方法の例があれば?

答えて

3

いくつかの調査の後、私は解決策を発見した:

錠剤は、デフォルトの向きとして景観を持っているので、向きが横長であり、スマートフォンは、肖像画を持っているときに、画面の回転が0又は180です。 Iは、錠剤やスマートフォンとの差に次のコードを使用します

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; 
} 

、Iは、デフォルトの向きが横の場合にのみ前として座標を再マッピング

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

よろしくお願いいたします。

+0

最後のCodeblockでこのnewm変数を取得しますか?ありがとうございました – vacetahanna

関連する問題