我正在尝试从 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;
}
}