简体   繁体   English

在横向和纵向模式下检测方向

[英]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 . 配置不支持ORIENTATION_REVERSE_LANDSCAPEORIENTATION_REVERSE_PORTRAIT

I have also tried Activityinfo.ORIENTATION_REVERSE_PORTRAIT but it's also not working.help me. 我也尝试过Activityinfo.ORIENTATION_REVERSE_PORTRAIT但是它也没有用。请帮助我。

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() . 我发现,如果您的设备具有磁力计,则可以使用SensorManager方法getRotationMatrix()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. 如果您的设备没有磁力计,则可以使用加速度计的x轴和y轴值区分横向和反向横向。 A negative x-axis value indicates reverse landscape (landscape right), and a positive value indicates landscape. x轴负值表示横向风景(右侧风景),正值表示风景。 Similarly, a negative y-axis value indicates reverse portrait, and a positive y-axis value indicates portrait. 类似地,负y轴值表示反向肖像,而正y轴值表示肖像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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