如何确定从传感器设备取向 [英] how to determine device orientation from the sensors
本文介绍了如何确定从传感器设备取向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的活动锁定在景观。但我仍需要知道设备的方向。所以,我选择使用的传感器。我有以下的code
My activity is locked on LANDSCAPE. But still I need to know the orientation of the device. So I have elected to use sensors. I have the following code
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_NORMAL);
...
@Override
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
// Movement
float azimuth = values[0];
float pitch = values[1];
float roll = values[2];
if ((-110 <= pitch && pitch <= -70) || (70 <= pitch && pitch <= 110)) {
//PORTRAIT MODE
portraitPitch = true;
landscapePitch = false;
Log.d(TAG, "portrait mode: pitch = " + pitch);
} else if ((-20 <= pitch && pitch <= 20) || (-200 <= pitch && pitch <= -160) || (160 <= pitch && pitch <= 200)) {
//LANDSCAPE MODE
portraitPitch = false;
landscapePitch = true;
Log.d(TAG, "landscape mode : pitch = " + pitch);
}
if ((-20 <= roll && roll <= 20)) {
//PORTRAIT MODE
portraitRoll = true;
landscapePitch = false;
Log.d(TAG, "portrait mode: roll = " + roll);
} else if ((-110 <= roll && roll <= -70) || (70 <= roll && roll <= 110)) {
//LANDSCAPE MODE
portraitRoll = false;
landscapePitch = true;
Log.d(TAG, "landscape mode : roll = " + roll);
}
if (portraitPitch && portraitRoll && !portrait) {
portrait = true;
landscape = false;
rotateIconsToPortraitMode();
Log.d(TAG, "portrait mode for icons: pitch = " + pitch + ", roll = " + roll);
}
if (landscapePitch && landscapeRoll && !landscape) {
landscape = true;
portrait = false;
rotateIconsToLandscapeMode();
Log.d(TAG, "landscape mode for icons: pitch = " + pitch + ", roll = " + roll);
}
}
我的code未工作可靠,事实上, otateIconsToLandscapeMode()
从未达到。
我如何使用约方位
,间距的信息
和卷
来确定设备的纵向或横向?`我希望我的问题是不够具体,保证一个明确的答案。谢谢你。
How do I use the information about azimuth
, pitch
, and roll
to determine the PORTRAIT or LANDSCAPE orientation of the device?` I hope my question is specific enough to warrant a specific answer. Thanks.
推荐答案
在的OnCreate
实例化一个 OrientationEventListener
OrientationEventListener orientationEventListener = new OrientationEventListener(this)
{
@Override
public void onOrientationChanged(int orientation)
{
Log.d(TAG, "orientation = " + orientation);
}
};
orientationEventListener.enable();
使用方向值可以判断是否在纵向或横向的设备
Using the orientation value you can determine whether the device in in Portrait or Landscape.
这篇关于如何确定从传感器设备取向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文