我正试图从Android方向获得倾斜天使。
我从
获得的价值观 SensorManager.getOrientation(mRotationMatrix,mValuesOrientation)
不是度数....将手机放在水平面上会返回不正确的值
我尝试使用网上找到的几种没有任何运气的方法。
到目前为止尝试了
Math.sin(Math.toRadians(mValuesOrientation [0]));
Math.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;
}
}
答案 0 :(得分:2)
getOrientation以Radian返回方位角,俯仰和滚动。
mValuesOrientation [0]是关于z轴的方位角旋转 mValuesOrientation [1]是关于x轴的俯仰,旋转 mValuesOrientation [2]是围绕y轴的滚动,旋转。
如果您的手机平躺在桌子上,则俯仰和滚动应该几乎为0,而不是方位角。如果您的手机较长的尺寸(y轴)指向磁北极,则仅为0。
您可以使用俯仰或滚动(取决于设备方向)来计算手机的倾斜度,即屏幕表面与世界xy平面之间的角度,或者您可以使用{{{{ 3}}
您还应该在How to measure the tilt of the phone in XY plane using accelerometer in Android阅读我的答案,以便更好地了解旋转矩阵。