安卓传感器直立旋转

问题描述:

我知道有关于此的线程,我一直在阅读它们好几天。安卓传感器直立旋转

我觉得我的问题更具体。我试图让设备旋转y轴。

如果我正确的,它被称为方位角

当运行应用程序,它经过调试记录值[0],值在logcat的返回0.0 [1],&值[2]

代码:

mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 
     accelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
     magnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); 

     SensorEventListener SEL = new SensorEventListener() { 
      @Override 
      public void onSensorChanged(SensorEvent event) { 

       mSensorManager.getRotationMatrix(rotationMatrix, I, gravity, geomagnetic); 

       mSensorManager.getOrientation(rotationMatrix, values); 

       accy = values[0]; 

       System.out.println(accy); 
      } 

      @Override 
      public void onAccuracyChanged(Sensor sensor, int accuracy) { 

      } 
     }; 

     int rate = SensorManager.SENSOR_DELAY_GAME; 
     mSensorManager.registerListener(SEL, accelerometer, rate); 
     mSensorManager.registerListener(SEL, magnetometer, rate); 

logcat的:

09-05 17:34:02.622 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.642 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.642 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.662 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.662 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.682 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.682 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.702 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.702 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.722 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.722 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.742 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.742 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.762 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.762 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.782 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.782 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.802 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 
09-05 17:34:02.802 6421-6421/com.ahewdev.eataround I/System.out﹕ 0.0 

从上面的代码参数gravitymSensorManager.getRotationMatrix(rotationMatrix, I, gravity, geomagnetic);geomagnetic可能是zero array,因此您accy总是zero

+0

确定。现在,这似乎很明显。我想我没有完全理解功能如此恶劣做一些研究。谢谢。 – ahew 2014-09-06 02:35:45

+0

看看http://*.com/questions/17979238/android-getorientation-azimuth-gets-polluted-when-phone-is-tilted/17981374#17981374 – 2014-09-06 02:55:12

+0

完美。这就是我一直在寻找的东西。 – ahew 2014-09-06 14:33:26