简体   繁体   English

如何检测屏幕方向变化? -Microsoft Surface上的JavaFX

[英]How to detect screen orientation change? - JavaFX on Microsoft Surface

I am writing a JavaFX application for Microsoft Surface and I am looking for a way to detect a screen orientation change when rotating the device from portrait to landscape and vice versa. 我正在为Microsoft Surface编写JavaFX应用程序,并且正在寻找一种将设备从纵向旋转到横向(反之亦然)时检测屏幕方向变化的方法。 The current method I am using to detect the screen orientation is as follows: 我用来检测屏幕方向的当前方法如下:

window.widthProperty().addListener((obs, oldVal, newVal) -> {
    if((double) newVal < window.getHeight()) {
        setPortraitMode();
    } else {
        setLandscapeMode();
    }
});

This method works fine for manual window resizing. 此方法适用于手动调整窗口大小。 However, the orientation change (device rotation) does not trigger a window resize event, so the method to change the layout will not fire automatically. 但是,方向更改(设备旋转)不会触发窗口调整大小事件,因此更改布局的方法不会自动触发。

What is the proper way to detect the screen orientation change without listening for resizing event? 在不侦听调整大小事件的情况下检测屏幕方向变化的正确方法是什么?

The solution to this issue is to check for a change in the aspect ratio. 解决此问题的方法是检查宽高比是否发生变化。 The condition I used: 我使用的条件:

    if((double) newVal < window.getHeight() || ((double) newVal/visualBounds.getHeight() < 1.5) 

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

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