我将做一些底层的渲染工作,但是我需要知道真正的显示DPI,才能制作出所有正确大小的东西。
我找到了一种方法来执行此操作:java.awt.Toolkit.getDefaultToolkit().getScreenResolution()
—但它在OS X上以“视网膜”显示返回错误的结果,它是实际DPI的1/2。(在我的情况下,应为220,但应为110)
因此,必须有其他一些更正确的API可用,或者我需要仅针对OS X实施hack —以某种方式查找当前显示是否为“视网膜”。但是我也找不到任何查询此信息的方法。有这个答案,但是在我的机器上Toolkit.getDefaultToolkit().getDesktopProperty("apple.awt.contentScaleFactor")
只是返回null。
我该怎么做?
看来目前有可能从中获取它java.awt.GraphicsEnvironment
。这是在最新的JDK(8u112)上运行的注释代码示例。
// find the display device of interest
final GraphicsDevice defaultScreenDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// on OS X, it would be CGraphicsDevice
if (defaultScreenDevice instanceof CGraphicsDevice) {
final CGraphicsDevice device = (CGraphicsDevice) defaultScreenDevice;
// this is the missing correction factor, it's equal to 2 on HiDPI a.k.a. Retina displays
final int scaleFactor = device.getScaleFactor();
// now we can compute the real DPI of the screen
final double realDPI = scaleFactor * (device.getXResolution() + device.getYResolution()) / 2;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句