我的活动已在LANDSCAPE上锁定。但我仍然需要知道设备的方向。所以我选择使用传感器。我有以下代码
var dlg=$('#createTeam').dialog({
title: 'Create a Team',
resizable: true,
autoOpen:false,
modal: true,
hide: 'fade',
width:600,
height:285,
close: function() {
window.location.reload();
}
});
我的代码无法可靠地工作,事实上,永远不会达到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);
}
}
。
如何使用有关otateIconsToLandscapeMode()
,azimuth
和pitch
的信息来确定设备的PORTRAIT或LANDSCAPE方向?我希望我的问题具体到足以保证具体答案。感谢。
答案 0 :(得分:3)
在oncreate
实例化OrientationEventListener
OrientationEventListener orientationEventListener = new OrientationEventListener(this)
{
@Override
public void onOrientationChanged(int orientation)
{
Log.d(TAG, "orientation = " + orientation);
}
};
orientationEventListener.enable();
使用方向值可以确定设备是纵向还是横向。
答案 1 :(得分:0)
如果您只需要LANDSCAPE与PORTRAIT方向,则音高值应该足够。根据您的具体需求,它可能并不完美。但根据我的理解,你可以简单地使用音调。所以就像你拥有它一样,只需忽略roll。