Temporarily, programmatically disable screen rotation in Qt - android

Please note: This question is about Qt C++ framework, not normal Java API. In Java, this question has already been answered.
In the main menu of my application, I really don't want to worry about different kinds of screen rotation. I would like to disable screen rotation until user goes to other views where screen rotation is meaningful. For main menu, I want to use portrait view only.
How to achieve that? How to control app screen rotation?

You can set the orientation using
QAndroidJniObject activity = QtAndroid::androidActivity();
activity.callMethod<void>("setRequestedOrientation", "(I)V", orientation);
where orientation is an int representing the orientation. You can copy the codes provided in the documentation (e.g., 0 to lock landscape, 1 to lock portrait and -1 to unlock any rotation), but I recommend to use, e.g.,
QAndroidJniObject::getStaticField<int>("android.content.pm.ActivityInfo", "SCREEN_ORIENTATION_LANDSCAPE");
to get the code values.
This answer is based on https://stackoverflow.com/a/38947312/4248972. The difference is that this answer also explains how to unlock rotation again and provides a sample to show how to get the orientation codes without manual copying.

Related

stage orientation in flash

I want to implement device rotation in my flash game running on AIR and Android. I use Starling, and when i rotate my device, Starling reinitializes the OpenGL device/context, this will destroy all textures and load them again after reinitialization, and time in which stage is rotated is too big, at least this is what i think it do.
The question is: how can i improve the time in which my stage is rotated ?
I think i can listen to StageOrientationEvent.ORIENTATION_CHANGING to prevent it from default behaviour, and rotate manually my game (rotate main sprite). But if i do this, and for example rotate my device from default orientation to left, then i can't rotate them back to default because the orientation state remains on default and event isn't dispatched. Can i somehow set the orientation state without actually orient the stage?
Did you tried to use:
Starling.handleLostContext = true
to handle lost Context?
This option should be enabled before you launch your Starling instance and it is the easiest way to automatically handle lost context.

Appunta AR markers overlaps, change the orientation of eyeview but make activity in portrait mode only

I am developing an application where I am using three views of the data.
1.) ListView 2.) ARView 3.) MapView.
For Geo-AR I am using "Appunta AR-engine".
Its very cool and simple AR-engine.
Previously I was using metaio SDK. But there is lot of problem with this SDK on many handsets.Here is the screen developed using Metaio.
....
This is the screen I have developed using Appunta AR-engine. But here evrything is collapsed.
Is there any way we can arrange them in visible order.
One more thing, when I make Activity as portrait only(in manifest).
AR eyeviw does not go to landscape mode.
How can we change the orientation of EYEView and radarview, by keeping the activity in portrait mode only.
screen developed using appunta
I'm the author of the appunta library :)
Currently the platform does not support ordered drawing by itself (noticed for future releases), but you can implement by yourself just sorting the points by distance before drawing them in the preRender method of the eyeview (this method is executed once per frame before drawing the points) or setting the z-order for each point while drawing them.
About the eyeView, this should work without issues in portrait mode, in fact, the sample is rotable.
I suspect you refere to the camera view, my advice is frankly do not use it. I had to implement a "rotable" camera view for project requirements, but it's horrid and it's a real resource eater. Just implement a simple non-rotable camera view like this.

Is there a way to do a "setRotation()" either with a method or code?

So I want my app to always lock onto the natural orientation of the device. So basically, I want to force the rotation to 0 degrees. There's a getRotation() method here:
http://developer.android.com/reference/android/view/Display.html
However, there is no setRotation(int rotation) that would accept one of the Surface.ROTATION_X parameters found here:
http://developer.android.com/reference/android/view/Surface.html
So, is there some way to do this with a single line of code that will always work? Or would this have to be done with some code?
The only thing I can think of as a manual solution is the following, but I have some concerns about it.
I can use the display.getRotation(). Then I'll check if it's 0 or 180 degrees, after that I'll get the display height and width, and see which one is longer than the other. If it appears I'm in landscape mode, then I'll know that the device's natural orientation is landscape so I could set it to landscape. However if the rotation was 270 or 90, then I would know the natural orientation is portrait, and I could set it to portrait.
However, my concern is where to do this such that it always works. Let's say the user is rotating the screen as this piece of code is executing, if the device changes orientation in between the time I use the getRotation() method, and check the width against the height with some if statements and what not, I think it could throw everything off. What happens if I put this in onCreate() or onResume()? Is it possible that things can get out of wack?
Is it possible for a the onCreate() or onResume() to be interrupted by a screen re-orientation due to the user turn the device around? The other issue is that if you flip the device by 180 degrees, the activity is never destroyed, so I think the behavior of this situation could very whether it's a 90 degree turn, or a 180 degree turn.
You can "set" the screen orientation in the manifest. See here: http://developer.android.com/guide/topics/manifest/activity-element.html#screen
<activity
android:name=".Activity"
android:screenOrientation="portrait"
android:theme="#style/Theme.D1"
android:label="#string/title_activity"/>
The value you're probably looking for is; "user" The user's current preferred orientation. However, I would test this on several test cases to be sure. HTH!

How do I detect screen rotation

I have an activity that is showing a compass, and I need to know the orientation of the screen to properly rotate the needle. I need to distinguish between 90° and 270° degree rotation to properly handle this.
When I rotate the screen (on my Nexus S at least) the activity's onCreate gets called, e.g. when I rotate from portrait to landscape mode. When I rotate from one landscape mode to the other with the top edge raised, onCreate() gets called twice (once for portrait orientation and once for the target landscape mode).
However, when I rotate with the bottom edge up, the screen rotates 180° and onCreate is not getting called. Is there an event that gets fired in this case?
You are correct that at a 180 degree rotation you will not get restarted or get a configuration change, because the configuration has not actually changed. (It is still in landscape or portrait, and everything else is the same.)
However if you are showing a compass, that must mean you are updating it from sensor events. Thus you can just call Display.getRotation() each time you get a sensor update to get the current rotation of the screen. In fact that you need is rotation of interpreting sensor events (or actually how your drawing of them will be modified when it gets to the screen), not just the orientation.
I found a relevant blog post that is well worth reading.
Be sure to read the SDK documentation.
Also check out the discussion about using Display.getRotation() to correctly remap sensor coordinates.
The helper class OrientationEventListener makes it very easy to get call backs on rotation. I would suggest you give it a try.
Just take a look at the SensorManager Documentation, there you can get the best examples and explanations of how to use the accelerometer to get the most precise informations. Instead of making the things just by detecting the phone orientation, with that API you can get the exactly rotation, just like used by racing games like Asphalt and Need For Speed.
As Monchote said, in order to make your needle rotate as expected on the compass, it's better to lock your UI to portrait or landscape, and change your needle angle according to the device rotation angle, which can be gained easily from OrientationListener.
In this way, no matter the auto rotation feature is opened or not by user, you can always get rotation angle change notification. Here's a good example for your reference.
Have a look at the OnConfigurationChanged method to track down the orientation change and re-arrange your UI as needed.
See some sample code

Android: Dynamically change location of Options Menu

I have a drawing activity that creates a custom 'canvas' View that occupies the entire screen, allowing the user to draw on it. I have a requirement to lock the Activity to the portrait orientation, as the drawing app is a sketchpad replacement, and the user would have the need to rotate the 'pad' to draw without the pad flipping around on them. This of course means that the Options Menu is locked to that orientation as well and depending on the user, the menu could be popping out of the top, bottom or sides. I'd like to have the Options Menu always be located at the bottom relative to the user... some options I am considering and would appreciate any suggestions or info:
Allow orientation changes, so the menu moves as needed, then use the SensorManager to listen for the orientation, and manually rotate the canvas View back to the 'correct' orientation (portrait). This would be ugly, as there could be a delay in the rotation of the canvas, and it introduces some potentially complex calculations, etc due to the X/Y being swapped.
Restrict orientation changes, then use the SensorManager to listen for the orientation, and manually rotate the Options Menu back to the bottom. No idea if this is possible.
Lock orientation, and create a custom View that intercepts the menu button and displays a 'fake' Options Menu that I need to manage, etc. Really really don't want to have to replace built-in OS functionality, could be a nightmare to maitain for different versions of OS in future, etc.
Anyone have any ideas on how to handle this? I'd love if #2 was possible, but can't find anything related to that.
Thanks,
Paul
EDIT:
Have confirmed that #2 is not possible, so I guess my options are #1, #3, unless anyone else has any ideas?
My solution is as follows:
Allow normal orientation changes in the Android Manifest for the Activity.
In the custom 'canvas' view constructor, fire up a tracking field, mDisplay, that gets the display information via:
Code:
mDisplay = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
In onDraw(), check to see if the display rotation is something other than 0, and rotate the canvas back to the 0 rotation value:
Code:
if (mDisplay.getRotation() > 0) {
canvas.save(Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
canvas.rotate(-mDisplay.getRotation(), px, py);
...
do stuff
...
canvas.restore();
}
Not sure that it's the best solution, but it seems to work.

Categories

Resources