问题描述
我想从Android的方向倾斜的天使。
I am trying to get the tilt angel from Android orientation.
这我是从结果得到的值
SensorManager.getOrientation(mRotationMatrix,mValuesOrientation)
结果
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation)
不度....在把手机上的一个水平面上则返回值不当
is not degrees.... On putting the phone on a horizontal plane it returns improper values
我在使用净找到一些方法,没有任何运气尝试。
I have tries using few methods found on net without any luck..
到目前为止已经试过
Math.sin(Math.toRadians(mValuesOrientation [0]));
Math.sin(Math.toRadians(mValuesOrientation[0]));
Math.toDegrees(mValuesOrientation [0]);
Math.toDegrees(mValuesOrientation[0]);
和其他
code低于
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返回弧度方位角,俯仰和横滚。
The getOrientation return the azimuth, pitch and roll in Radian.
mValuesOrientation [0]是方位角,绕z轴。结果
mValuesOrientation [1]是音调,绕x轴。结果
mValuesOrientation [2]为辊,绕y轴。
mValuesOrientation[0] is the azimuth, rotation about the z-axis.
mValuesOrientation[1] is the pitch, rotation about the x-axis.
mValuesOrientation[2] is the roll, rotation about the y-axis.
如果你的手机是平躺在桌子上,俯仰和滚转应该是几乎为0,但不能方位。只有0如果你的手机不再大小,y轴指向正是向着磁北。
If your phone is lying flat on a table, the pitch and roll should be almost 0, but not the azimuth. It is only 0 if your phone longer size, the y axis, is pointing exactly toward magnetic North.
您可以使用俯仰或横滚(取决于设备的方向)来计算手机的倾斜,也就是屏幕的表面和世界xy平面或者你可以使用我的答案在倾斜How在Android中使用加速度计来测量手机在XY平面倾斜
You can use pitch or roll (depending on the device orientation) to calculate the inclination of the phone, ie the angle between the surface of the screen and the world xy plane or you can use the inclination of my answer at How to measure the tilt of the phone in XY plane using accelerometer in Android
您也应该在Convert磁场X,Y,从设备到全球参照系 Z值以获得更好的理解旋转矩阵的。
You should also read my answer at Convert magnetic field X, Y, Z values from device into global reference frame to get a better understanding of the rotation matrix.
这篇关于无法从getOrientation方法获取正确度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!