Appcelerator Titanium app, asking specifically about Android
Our app is locked to portrait mode:
android:screenOrientation="nosensor"
is in the tiapp.xmlorientationModes: [Ti.UI.Portrait]
setYet, when we show the camera (with an overlay), it is permitted to rotate. This means user photos can end up sideways or upside down. Unfortunately, because the app is locked to portrait mode, Ti.Gesture.orientation
and myWindow.orientation
always returns 1 (portrait) so we can't manually de-rotate the image.
How can I either a) lock the orientation of the camera, or b) find the actual device orientation so I can manually de-rotate the image?
The answer is to use the accelerometer.
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);
});
Just keep in mind that the accelerometer listener fires like a bajillion times a second and is another drain on the battery. Make sure to remove the update
listener as soon as you can.