Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 无法从getOrientation方法获取正确的度数_Android_Orientation_Android Sensors - Fatal编程技术网

Android 无法从getOrientation方法获取正确的度数

Android 无法从getOrientation方法获取正确的度数,android,orientation,android-sensors,Android,Orientation,Android Sensors,我正在尝试从Android方向获取倾斜角度 我从 SensorManager.getOrientation(mRotationMatrix、mValuesOrientation) 不是学位。。。。将手机放在水平面上时,会返回不正确的值 我尝试过使用一些在网上找到的方法,但没有任何运气 到目前为止 Math.sin(Math.toRadians(mValuesOrientation[0]) 数学toDegrees(mValuesOrientation[0]) 及其他 代码如下 public cl

我正在尝试从Android方向获取倾斜角度

我从

SensorManager.getOrientation(mRotationMatrix、mValuesOrientation)

不是学位。。。。将手机放在水平面上时,会返回不正确的值

我尝试过使用一些在网上找到的方法,但没有任何运气

到目前为止

Math.sin(Math.toRadians(mValuesOrientation[0])

数学toDegrees(mValuesOrientation[0])

及其他

代码如下

public class MainActivity extends Activity  {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        

    SensorManager sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);        

    final float[] mValuesMagnet      = new float[3];
    final float[] mValuesAccel       = new float[3];
    final float[] mValuesOrientation = new float[3];
    final float[] mRotationMatrix    = new float[9];

    final Button btn_valider = (Button) findViewById(R.id.button1);
    final TextView txt1 = (TextView) findViewById(R.id.editText1);
    final SensorEventListener mEventListener = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
        }

        public void onSensorChanged(SensorEvent event) {
            // Handle the events for which we registered
            switch (event.sensor.getType()) {
                case Sensor.TYPE_ACCELEROMETER:
                    System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
                    break;

                case Sensor.TYPE_MAGNETIC_FIELD:
                    System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
                    break;
            }
        };
    };

    // You have set the event lisetner up, now just need to register this with the
    // sensor manager along with the sensor wanted.
    setListners(sensorManager, mEventListener);

    btn_valider.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
            SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
            String test;
           /* double accX = -mValuesOrientation[0]/SensorManager.GRAVITY_EARTH;
            double accY = -mValuesOrientation[1]/SensorManager.GRAVITY_EARTH;
            double accZ = -mValuesOrientation[2]/SensorManager.GRAVITY_EARTH;
            double totAcc = Math.sqrt((accX*accX)+(accY*accY)+(accZ*accZ));
            double tiltX = Math.asin(accX/totAcc);
            double tiltY = Math.asin(accY/totAcc);
            double tiltZ = Math.asin(accZ/totAcc);*/
            //float tiltX = mValuesOrientation[0] * 57.2957795f;
            //float tiltY = mValuesOrientation[1] * 57.2957795f;
            //float tiltZ = mValuesOrientation[2] * 57.2957795f;
            //double tiltX =Math.sin(Math.toRadians(mValuesOrientation[0]));
            //double tiltY =Math.sin(Math.toRadians(mValuesOrientation[1]));
            //double tiltZ =Math.sin(Math.toRadians(mValuesOrientation[2]));
            //String.format("Azimuth: %.2f\n\nPitch:%.2f\nRoll", azimuth,
            //        pitch, roll);
            double tiltX = Math.toDegrees(mValuesOrientation[0]);
            double tiltY = Math.toDegrees(mValuesOrientation[1]);
            double tiltZ = Math.toDegrees(mValuesOrientation[2]);
            test = "results New: " +tiltX +" "+tiltY+ " "+ tiltZ;
            Log.d("test", test);
            txt1.setText(test);
        }
    });


}
public void setListners(SensorManager sensorManager, SensorEventListener mEventListener)
{
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), 
            SensorManager.SENSOR_DELAY_NORMAL);
    sensorManager.registerListener(mEventListener, sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD), 
            SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

getOrientation以弧度返回方位角、俯仰和滚动

mValuesOrientation[0]是方位角,绕z轴旋转。
mValuesOrientation[1]是围绕x轴的节距和旋转。
mValuesOrientation[2]是围绕y轴的滚动和旋转

如果你的手机平放在桌子上,俯仰和滚动应该几乎为0,而不是方位角。如果你的手机长一点,y轴正好指向磁北,那么它只有0

您可以使用“俯仰”或“滚动”(取决于设备方向)来计算手机的倾斜度,即屏幕表面与世界xy平面之间的角度,也可以使用“我的答案”的倾斜度


您还应该阅读我的答案,以便更好地理解旋转矩阵。

您所说的倾斜是什么意思?倾斜。从手机的wrt到X Y Z轴。。。。所以,如果我把手机放在桌面上,所有的x,y,z值都应该是0,感谢你指出单位是弧度。这与提供方位角/俯仰角/滚动角度的
TYPE_方位传感器不同。如果你不阅读API文档中的细节,这将是一个很大的陷阱……文档声称音高在-180到180之间变化,但似乎在-90到90之间。你知道为什么会这样吗?