简体   繁体   中英

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. 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) 

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