Search code examples
androidscreen-orientation

Display.getRotation() not exactly working for 180 degree flips of the device


I'm using Display.getRotation() and simply displaying the rotation with a toast message in my onCreate() method since onCreate() is called every time you rotate the screen.

Anyway, I discovered some odd behaviour which I think is best explained with an example.

Let's say I'm holding the device in it's "natural" rotation of 0 degrees. If I rotate 90 degrees clock wise I get 270 degrees, and if I rotate 90 degrees counter-clock wise I get 90 degrees. However, if I quickly rotate the device 180 degrees, that is fast enough so that there is no transition through the 90 degree rotation, or the 270 degree rotation, the entire image does get rotated, but the rotation still gets reported as the original 0 degrees.

Let's say I were holding at 90 degrees, it works just fine if I rotate 90 degrees in either direction, but if I do the 180 degree flip, again, the image rotates, but it still reports the original 90 degrees.

What's the deal here? Any ideas?

I'm going to try some more experimentation, if I figure it out, I'll post up the results.


Solution

  • From the documentation:

    public int getRotation ()
    

    Since: API Level 8 Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

    I hope this clarifies some things for you.