I need to read the pitch value (how much the phone is tilted backwards and forwards) both in portrait and landscape modes. using the code bellow in portrait I get my value from value[1] with 0.0 when phone stay laying flat with face up, -90 when standing upright and 180 when lying flat on the device face. All great till now…
The problem comes when the device is in landscape mode. At this point I’m using value[2] to measure device tilt, but the problem is with the values : 0 when the phone stay laying flat (OK) rise to 90 when it is standing upright (OK), but when I continue the movement the value drop again below 90 (80, 75, etc…), so basically I can’t differentiate between these 2 positions as the values are identical.
So, what I’m doing wrong, what other values from the sensors I can read in order to have a full picture of the device tilt both in landscape and portrait mode ?
Same questoion as here: http://groups.google.com/group/android-beginners/browse_thread/thread/c691bbac3e294c7c?pli=1
I Have the following code :
private void ReadOrientationSensor(){
final SensorManager sensorManager;
final TextView text = (TextView) this.findViewById(R.id.TextView01);
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
SensorEventListener listener = new SensorEventListener() {
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
float x,y,z;
x=event.values[0];
y=event.values[1];
z=event.values[2];
//text.setText(String.valueOf(event.values[0]));
text.setText("x: " + x + " y: " + y + " z: " + z);
}
};
sensorManager.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_FASTEST);
}
Sensor.TYPE_ORIENTATIONis deprecated and should not be used.Reading the device orientation gave me some headache as well. Here is a base class that I am using for activities that need the device’s orientation:
Orientation goes like this:
You need to call
setAxisMapping()to receive the orientation aligned to either portrait or landscape mode. I only called it from within the constructor so I cannot tell you what happens when you call it while the activity is running. You might have to reset the matrixes.