1

I am trying to create an app that will run on both PC (Windows) and Android, however, I am having issues getting the correct screen size on both.

On Windows, the Screen.getBounds() method seems to always return the correct full screen size (i.e. the screen without the taskbar space etc). On Android, using Screen.getBounds() will return a screen size which is much larger than the actual screen size. The only way I can get my app to work correctly on android is to use Screen.getVisualBounds(). However, on Windows, using Screen.getVisualBounds() always returns a size slightly smaller in height than the actual total screen size since it removes the space occupied by the taskbar.

Does anyone know why the Screen.getBounds() returns a much higher value on Android than the actual visible screen?

Thanks.

4

1 回答 1

2

Screen.getBounds()返回物理像素,同时Screen.getVisualBounds()返回逻辑像素。

虽然在桌面上,这些边界之间的差异仅与任务栏的存在有关,但在移动设备上,差异与像素密度或比例有关,并且可以大于 1。

这是这些方法在 Nexus 6 上返回的内容:

关系6

由于该设备的像素密度为3.5。

回到你最初的问题,你需要Screen.getVisualBounds()在 Android 上使用。

但是对于桌面,您可以自由选择大小:

@Override
public void start(Stage stage) {
    Rectangle2D bounds = JavaFXPlatform.isDesktop() ? 
            Screen.getPrimary().getBounds() : 
            Screen.getPrimary().getVisualBounds();
    Scene scene = new Scene(new StackPane(), bounds.getWidth(), bounds.getHeight());

    stage.setScene(scene);
    stage.show();
}

哪里JavaFXPlatform来自 Gluon Charm Down,一个 OSS 库,你可以在这里找到

于 2016-04-09T10:16:20.820 回答