【问题标题】:Titanium: App locked to portrait, yet camera rotates, how to determine actual device orientation?Titanium:应用程序锁定为纵向,但相机旋转,如何确定实际设备方向?
【发布时间】:2016-05-19 14:44:50
【问题描述】:
Appcelerator Titanium 应用,专门询问 Android
我们的应用被锁定为纵向模式:
-
android:screenOrientation="nosensor" 在 tiapp.xml 中
- 所有窗口都设置了
orientationModes: [Ti.UI.Portrait]
然而,当我们展示相机(带有覆盖)时,它是允许旋转的。这意味着用户照片最终可能会横向或上下颠倒。不幸的是,由于应用被锁定为纵向模式,Ti.Gesture.orientation 和 myWindow.orientation 始终返回 1(纵向),因此我们无法手动取消旋转图像。
我如何 a) 锁定相机的方向,或 b) 找到实际的设备方向,以便我可以手动取消旋转图像?
【问题讨论】:
标签:
titanium
titanium-mobile
appcelerator-titanium
【解决方案1】:
答案是使用加速度计。
function accelerometerCallback(e) {
var deviceOrientation;
// Get the current device angle
var xx = -e.x;
var yy = e.y;
var angle = Math.atan2(yy, xx);
if (angle >= -2.25 && angle <= -0.75) {
deviceOrientation = "portraitUpsideDown";
} else if (angle >= -0.75 && angle <= 0.75) {
deviceOrientation = "landscapeRight";
} else if (angle >= 0.75 && angle <= 2.25) {
deviceOrientation = "portrait";
} else if (angle <= -2.25 || angle >= 2.25) {
deviceOrientation = "landscapeLeft";
}
console.log('ACCELEROMETER: orientation = ' + deviceOrientation);
}
Ti.Accelerometer.addEventListener('update', accelerometerCallback);
myWin.addEventListener('close', function () {
Ti.Accelerometer.removeEventListener('update', accelerometerCallback);
});
请记住,加速度计侦听器每秒会触发数十亿次,并且会再次消耗电池电量。请务必尽快删除 update 侦听器。