I am struggling with a quite rare problem.
Here it goes:
I have a SurfaceView and there is a JNI-side thread that handling the rendering operations on it (Let say Render-thread). For "some" reason I need to rotate my drawings by myself inside the render-thread. To achieve this, the render thread queries the rotation status from the android framework. And I can see the rotated screen whenever the device is rotated, but there is a problem: if I rotate the device, the screen rendered by Render-Thread is rotated, but Android UIs (such as notification bar, toast message, etc) are not rotated (Of course!). To resolve this problem, I use setRequestedOrientation function to tell the Android to rotate my Activity. Then I can also see the properly rotated Android UIs. At this moment, my real problem happens: The race between Render-Thread and Android's screen rotation stuff (I guess surface flinger?).
Apparently, if I call setRequestedOrientation function, Android framework rotates the screen by itself, while my Render-thread is also drawing the rotated screen. And this results in a glitch.
My question here is that:
Is there a way to tell Android framework not to rotate the SurfaceView while I'm still calling setRequestedOrientation?
Related
I am using SurfaceView with ndk+openGL, when press screen rotate button of the emulator,
i received SurfaceChanged event from SurfaceHolder.Callback, then i post a redraw message via Handler, and the Handler redraws the screen with new display metrics via JNI native code, but the display is broken, as the picture shows.
I sure that the native OpenGL drawing code already draws one frame, after press rotate button and before screen rotation finish, but don't know why the display is broken.
I'm not so familiar with android, could someone help me. thanks.
EDIT:
when i make some touch events to trigger new frames, the display got fixed.
so i'm thinking is there a rotate finish event, so i can force draw one frame to fix this issue.
Problem solved.
Override the surfaceRedrawNeeded method of SurfaceHolder.Callback2, it will be called after rotate finished.
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.
I try to use a Camera inside my app and I have a problem with proper handling of configuration changes. I know the proper way of handling configuration changes in a typical Activity (onSaveInstanceState etc...), but it's not the case here.
Think of my Activity as a fullscreen camera preview with an overlay shutter button and a frame which will be used to crop the image
What I'm trying to achieve (preview of a stock HTC Sense camera): http://youtu.be/uzfUJUNHY68
That is: the camera preview which is on TextureView should not be recreated on rotation, but the Activity should be orientation-aware, to position elements.
I'm pretty sure I don't want the Activity to be recreated on rotation. So I tried the android:configChanges approach. The problem is that onSurfaceTextureSizeChanged() is still called and the camera preview is rotated and the whole experience is not smooth (the preview lags for a moment and gets "recreated" or something).
I tried locking my Activity to landscape via android:screenOrientation="landscape", but this way onConfigurationChanged() is not called and I don't know how to position other views like in the video above. I guess I could listen to some accelerator events, but I'm pretty sure there is a better way. Is there?
Android camera source.
Lock activity rotation. On method onOrientationChanged of MyOrientationEventListener you can rotate, change positions of views etc. I think it's a best way.
Try with this option.
android:configChanges="keyboardHidden|orientation|screenSize"
I am writing an Android application that needs to rotate the screen as many times as possible in a certain period of time.
I can rotate the screen fine with setRequestedOrientation(int). The problem I am having is that if I try and rotate the screen in the last call to Activity, onWindowFocusChanged(boolean), the screen cannot keep up with the amount of times setRequestedOrientation is called.
My logcat is showing me that I am getting roughly 5-10 "Setting rotation to [0|1|2|3]" before I get one complete screen rotation.
Here's what I have tried:
Using view to request focus and using a OnRequestFocusChanged Listener
Rotating screen in a child thread to run runOnUiThread to add to main thread message queue
View.post
I know iv'e tried other stuff, but I forget, those are the most recent, and as you can tell I'm getting desperate.
So, is there a way to make sure that the Activity has fully rotated and is accepting user input? Maybe there is a way to check and see if the Activity is looping and waiting for input? Any help here would be absolutely great! Thanks guys.
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