java JavaFX 8 HiDPI 支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26182460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:27:26  来源:igfitidea点击:

JavaFX 8 HiDPI Support

javajavafx-8hdpi

提问by tryzor

I just tried out the JavaFX Hello World Exampleon a 4k screen on Arch Linux, but unfortunately the GUI does not scale.

我刚刚在 Arch Linux 的 4k 屏幕上试用了JavaFX Hello World 示例,但不幸的是 GUI 无法缩放。

The documentationsays

文件说:

Hi-DPI support. JavaFX 8 now supports Hi-DPI displays.

高 DPI 支持。JavaFX 8 现在支持 Hi-DPI 显示。

So how can I make my application dpi aware?

那么我怎样才能让我的应用程序知道 dpi 呢?

回答by jewelsea

Hi-DPI support on various devices

各种设备上的 Hi-DPI 支持

For OS X Macs with retina display it should "just work" - JavaFX is aware of Hi-DPI Macs and will scale the UI appropriately. If you set the spacing in a VBox to 8, then that is a device independent unit; on a non-retina display mac it will take up 8 pixels, on a retina display which has double the resolution, the spacing will take up 16 pixels. Because the retina display also has twice the DPI as well as twice the resolution of the non-retina display, the physical screen measurement of the space will be the same regardless of device.

对于带有视网膜显示的 OS X Mac,它应该“正常工作”——JavaFX 知道 Hi-DPI Mac 并将适当地缩放 UI。如果将VBox中的间距设置为8,则为设备无关单位;在非视网膜显示器的 Mac 上,它将占用 8 个像素,在分辨率翻倍的视网膜显示器上,间距将占用 16 个像素。由于视网膜显示器的 DPI 和分辨率是非视网膜显示器的两倍,因此无论设备如何,空间的物理屏幕测量都是相同的。

For Windows and Linux devices, your results may be less satisfactory as JavaFX 8u20 does not currently by default work out arbitrary DPI resolutions on such devices and scale to them appropriately. What you could do is perform most of your measurements in css as em units(which are based on the point size of the scene's root's default font) and similarly for fxml, and then set the point size of the scene root's default font appropriately depending on what you determine from querying the screen's DPI resolution. See the discussion in this answer for further information and sample code: javafx automatic resizing and button padding.

对于 Windows 和 Linux 设备,您的结果可能不太令人满意,因为 JavaFX 8u20 当前默认情况下不会在此类设备上计算任意 DPI 分辨率并适当地缩放到它们。您可以做的是在css 中以 em 单位(基于场景根默认字体的点大小)执行大部分测量,对于 fxml 也类似,然后根据以下情况适当设置场景根默认字体的点大小您通过查询屏幕的 DPI 分辨率确定的内容。有关更多信息和示例代码,请参阅此答案中的讨论:javafx 自动调整大小和按钮填充

Specific to Gnome

特定于 Gnome

Gnome 3 has a setting for the scaling factorwhich can be controlled by this command:

Gnome 3 有一个缩放因子设置,可以通过这个命令控制:

gsettings set org.gnome.desktop.interface scaling-factor 2

You can query this scaling factor by reading the user's gnome profile settings and use this in conjunction with querying the screen DPI to work out how an appropriate scaling factor then apply the scaling using the techniques described above.

您可以通过读取用户的 gnome 配置文件设置来查询此缩放因子,并将其与查询屏幕 DPI 结合使用,以计算出适当的缩放因子如何使用上述技术应用缩放。

Just a personal anecdote - when I tried using Gnome 3 scaling (CentOS 7 and also a recent Fedora release) on a Hi-DPI display a couple of days back, I found the overall support for Hi-DPI across applications running under Linux to be pretty spotty. Certainly, the support was much improved from CentOS 6 when I attempted that, but there was still quite a way to go to achieve quality Hi-DPI support across windowing toolkit, standard apps and third party apps. For this reason, I believe that running HiDPI Gnome desktops is still quite a bleeding edge thing which is definitely not for everyone - I am sure that this situation will change over time.

只是个人轶事 - 几天前,当我尝试在 Hi-DPI 显示器上使用 Gnome 3 缩放(CentOS 7 和最近的 Fedora 版本)时,我发现在 Linux 下运行的应用程序对 Hi-DPI 的整体支持是相当参差不齐。当然,当我尝试这样做时,CentOS 6 的支持得到了很大改善,但是要跨窗口工具包、标准应用程序和第三方应用程序实现高质量的 Hi-DPI 支持,还有很长的路要走。出于这个原因,我相信运行 HiDPI Gnome 桌面仍然是一件非常前沿的事情,这绝对不适合所有人——我相信这种情况会随着时间的推移而改变。

Bitmapped Images

位图图像

From a JavaFX team lead blog entry on Hi-DPI:

来自 JavaFX 团队负责人关于 Hi-DPI 的博客条目

In Apple's applications (starting with the iPhone and iPad with their retina displays), the solution to the problem is for the application developer to supply two images instead of one for each image asset. For example, the splash screen will be supplied with two images, one at normal resolution and one at 2x the resolution. The files are named the same but the 2x one is named according to some convention, such that at runtime the platform will lookup the 2x version on retina behind the scenes. In such a way, your application says “fooImage.png” but “[email protected]” is looked up instead when on a machine with a retina display.

在 Apple 的应用程序中(从带有视网膜显示屏的 iPhone 和 iPad 开始),问题的解决方案是让应用程序开发人员提供两张图像,而不是为每个图像资产提供一张图像。例如,初始屏幕将提供两张图像,一张为正常分辨率,另一张为 2 倍分辨率。这些文件的名称相同,但 2x 的名称是根据某种约定命名的,这样在运行时平台将在后台在视网膜上查找 2x 版本。通过这种方式,您的应用程序会显示“fooImage.png”,但在具有视网膜显示屏的机器上查找“[email protected]”。

I do not know whether this bitmapped image choosing functionality for Hi-DPI displays is currently in Java 8u20 or not - you might have to implement it yourself by querying the screen with screen.getDpi(), then loading the appropriate bitmap.

我不知道这个用于 Hi-DPI 显示的位图图像选择功能当前是否在 Java 8u20 中 - 您可能必须通过使用screen.getDpi()查询屏幕来自己实现它,然后加载适当的位图。

4K Devices

4K 设备

4K is a lot of pixels to push. JavaFX will by default use hardware a accelerated graphics pipeline when such a graphics pipeline is available. Some graphics hardware may not be fully optimized for 4K display (e.g. not enough video ram), which might lead to an application which either does not work or performs poorly. I also don't believe that currently a lot of effort has gone into investigating JavaFX performance on various 4K devices - it might "just work", but it might not either. You will need to test your application on the target hardware to determine the current capabilities of JavaFX applications when running on that hardware. You might also need to tweak the application according to some of the suggestions above.

4K 需要大量像素。当此类图形管道可用时,JavaFX 将默认使用硬件加速图形管道。某些图形硬件可能未针对 4K 显示进行完全优化(例如,视频内存不足),这可能会导致应用程序无法运行或性能不佳。我也不相信目前在调查各种 4K 设备上的 JavaFX 性能方面付出了很多努力——它可能“正常工作”,但也可能不会。您需要在目标硬件上测试您的应用程序,以确定 JavaFX 应用程序在该硬件上运行时的当前功能。您可能还需要根据上面的一些建议调整应用程序。

A user has reported an issue with JavaFX 8u20 when attempting to display a 4K video using JavaFX:

用户在尝试使用 JavaFX 显示 4K 视频时报告了 JavaFX 8u20 的问题:

Background

背景

Supporting Hi-DPI under OS X was (likely) simpler than Windows/Linux devices as the target devices are either retina or non-retina display with one being an exact 2x scale of the other and direct support from the OS X system can be leveraged to help achieve the retina scaling. With Windows/Linux, probably what is required is an ability to scale at factors other than just 2x, and that is covered by the (currently outstanding and scheduled) feature request RT-32521 Support global coordinate scaling with DPI-based default. Scaling by an integral amount usually gives the best visible results.

在 OS X 下支持 Hi-DPI(可能)比 Windows/Linux 设备更简单,因为目标设备是视网膜或非视网膜显示器,其中一个是另一个精确的 2 倍,并且可以利用 OS X 系统的直接支持以帮助实现视网膜缩放。对于 Windows/Linux,可能需要的是能够以 2以外的其他因子进行缩放,这由(当前未完成和计划的)功能请求RT-32521 支持全局坐标缩放与基于 DPI 的默认值涵盖。按整数缩放通常会给出最好的可见结果。

Additional Resources

其他资源

A complete guide to coding for Hi-DPI devices is outside the scope of this particular answer - you can google various web resources to get more information.

为 Hi-DPI 设备编码的完整指南不在此特定答案的范围内 - 您可以谷歌各种网络资源以获取更多信息。

If you have further questions on Hi-DPI support for JavaFX, I suggest you ask them on the openjfx-dev JavaFX developer mailing list.

如果您对 JavaFX 的 Hi-DPI 支持还有其他问题,我建议您在openjfx-dev JavaFX 开发人员邮件列表上询问。

Wiki Answer

维基答案

This answer may have some possible inconsistencies or errors and may date over time. I have made the answer community wiki. If you are aware of specific corrections, device and OS limitations or support model support for Hi-DPI on JavaFX, please feel free to edit this answer or move it to the OpenJFX wiki(where it probably belongs anyway).

这个答案可能有一些可能的不一致或错误,并且可能会随着时间的推移而过期。我已经制作了答案社区维基。如果您知道 JavaFX 上对 Hi-DPI 的特定更正、设备和操作系统限制或支持模型支持,请随时编辑此答案或将其移至OpenJFX wiki(无论如何它可能都属于此)。

回答by Maksud Chowdhury

I faced the same problem. I built javaFX application in my surface pro 3, which did not scale but the fxml apps did. then I installed jre8u221 withiout replacing the updated version it suggested. It fixed the problem

我遇到了同样的问题。我在 Surface pro 3 中构建了 javaFX 应用程序,它没有扩展,但 fxml 应用程序可以扩展。然后我安装了 jre8u221 而没有替换它建议的更新版本。它解决了问题