getOrientation()Pitch反映了地平线

时间:2012-08-14 17:19:57

标签: android sensor compass-geolocation pitch

我注意到当我把手机放在地平线以下时,我得到了> -90。当我开始倾斜手机使其指向天空时,它会反射到-90左右,即-88,-90,-88。当它从地面倾斜到天空。

以前有没有人经历过这个。 (它似乎与remapCoordinateSystem无关)。 (我之前评论过它)。当手机摄像头指向地面时,音高读数为零。当它指向天花板时它也是零。

感谢您的帮助。

    @Override
    public void onSensorChanged(SensorEvent event) {
        synchronized (MainActivity.this) { // TilteController
            switch (event.sensor.getType()) {
            case Sensor.TYPE_MAGNETIC_FIELD:
                mMagneticValues = event.values.clone();
                break;
            case Sensor.TYPE_ACCELEROMETER:
                mAccelerometerValues = event.values.clone();
                break;
            }

            if (mMagneticValues != null && mAccelerometerValues != null) {
                SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);

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

                switch (rotation)
                {
                case Configuration.ORIENTATION_LANDSCAPE:
                    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_MINUS_Y, SensorManager.AXIS_MINUS_X, R);//shouldnt be the same R in and out
                case Configuration.ORIENTATION_PORTRAIT:
                    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_Y, SensorManager.AXIS_X, R);//shouldnt be the same R in and out
                }


                float[] orientation = new float[3];
                SensorManager.getOrientation(R, orientation);

                mAzimuth = orientation[0];
                mPitch = orientation[1];
                mRoll = orientation[2];

                dirText.setText("Azimuth, Pitch, Roll || " + radStr(mAzimuth) +", "+ radStr(mPitch) +", "+ radStr(mRoll));

                glView.rotate((float)Math.toDegrees(mAzimuth),(float)Math.toDegrees(mPitch),(float)Math.toDegrees(mRoll));
                //glView.azimuth=(float)Math.toDegrees(smooth(mAzimuth));
                glView.pitch=(float)(Math.toDegrees(smooth(mPitch)));//-90 makes it cenetr
                //glView.roll=(float)Math.toDegrees(smooth(-mRoll));
                //Log.i("Azimuth, Pitch, Roll", mAzimuth+", "+mPitch+", "+mRoll);
            }
        }
    }

我需要的临时修复包括在调用get orientation之前旋转矩阵。

Matrix.rotateM(R, 0, 90, 0, 1, 0);

如果你完全翻转,它会遇到同样的问题。 (这应该通过添加方位角来解决)但它不是一个很好的解决方案。

所以,如果其他人正在阅读这个并试图使地平线0度。之前旋转矩阵,与旋转显示器相反(我使用OpenGL)

1 个答案:

答案 0 :(得分:1)

结束在横向上锁定应用程序并应用以下

    SensorManager.getRotationMatrix(R, null, mAccelerometerValues, mMagneticValues);

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

    SensorManager.remapCoordinateSystem(R, SensorManager.AXIS_X, SensorManager.AXIS_Z, R);//shouldnt be the same R in and out

    float[] orientation = new float[3];
    SensorManager.getOrientation(R, orientation);