简体   繁体   中英

Detect orientation in reverse landscape and portrait mode

I want to detect reverse landscape and portrait orientation. Configuration does not support ORIENTATION_REVERSE_LANDSCAPE or ORIENTATION_REVERSE_PORTRAIT .

I have also tried Activityinfo.ORIENTATION_REVERSE_PORTRAIT but it's also not working.help me.

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){

    Log.e("On Config Change","LANDSCAPE");
}else{

    Log.e("On Config Change","PORTRAIT");
}

I found that if your device has a magnetometer you can differentiate between landscape and reverse landscape by calculating the rotational attitude of the device (azimuth, pitch, and roll) using the SensorManager methods getRotationMatrix() and getOrientation() . If the roll is positive, you're in reverse landscape (landscape right), and if the roll is negative you're in landscape (landscape left).

Similarly, you can use the pitch to differentiate between portrait and reverse portrait. If the pitch is positive, you're in reverse portrait, and if the pitch is negative you're in portrait.

If your device doesn't have a magnetometer, you can use the x-axis and y-axis values of the accelerometer to differentiate between landscape and reverse landscape. A negative x-axis value indicates reverse landscape (landscape right), and a positive value indicates landscape. Similarly, a negative y-axis value indicates reverse portrait, and a positive y-axis value indicates portrait.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM