横向模式下的俯仰问题
我需要读取纵向和横向模式下的俯仰值(手机前后倾斜的程度)。使用肖像中的以下代码,当手机面朝上平放时,我从 value[1] 中获取值 0.0,直立时为 -90,平放在设备面上时为 180。到目前为止一切都很好... 当设备处于横向模式时就会出现问题。此时,我正在使用值 [2] 来测量设备倾斜度,但问题在于值:当手机平放时(正常)为 0,当手机直立时(正常)上升为 90,但是当我继续时运动值再次下降到 90 以下(80、75 等),所以基本上我无法区分这两个位置,因为值是相同的。 那么,我做错了什么,我可以从传感器读取哪些其他值,以便在横向和纵向模式下获得设备倾斜的全貌?
与此处相同的问题:http://groups.google。 com/group/android-beginners/browse_thread/thread/c691bbac3e294c7c?pli=1
我有以下代码:
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sensor.TYPE_ORIENTATION
已弃用,不应使用。阅读设备方向也让我有些头痛。下面是我用于需要设备方向的活动的基类:
方向如下:
您需要调用 setAxisMapping() 来接收与纵向或横向模式对齐的方向。我只从构造函数中调用它,因此我无法告诉您在活动运行时调用它时会发生什么。您可能必须重置矩阵。
Sensor.TYPE_ORIENTATION
is 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.