问题
I need to detect reverse landscape orientation in order to prevent user to start capturing vertical videos.
I've implemented 2 layouts in layout-land
and layout-port
folders with different set of controls. The problem is that Android doesn't allow me to create a separate layout specifically for reverse landscape. I need this because video is recorded upside down in this case, albeit UI is displayed correctly.
If I rotate device from 0 to 180 orientation Android doesn't even call onCreate()
of my activity, but video becomes flipped in this case.
Can somebody suggest a working example how to detect certain device orientation (reverse landscape in my case) to use another layout for this orientation?
回答1:
Use an OrientationEventListener to get the orientation in degrees of the phone.
Once you create a listener and call enable()
, onOrientationChanged(int) will be called whenever the phone's orientation is changed.
You can then check if it is out of the ranges you want the phone to be in.
Don't forget to use SENSOR_DELAY_NORMAL as your rate. This way it will only be called when an orientation change happens that you actually care about, not the small changes that usually happen while a user is handling their phone.
来源:https://stackoverflow.com/questions/18052035/android-detect-reverse-landscape-orientation