简体   繁体   English

系统托盘图标看起来扭曲

[英]System Tray icon looks distorted

I'm trying to have an icon be added and displayed to the system tray using Java. 我正在尝试使用Java添加一个图标并显示到系统托盘中。 However the icon is always either too small, or its cut off in areas. 但是,图标总是太小,或者在区域中被切断。 Its the second one from left in case you couldn't tell. 它是左起第二个你无法分辨的情况。

What am I doing wrong here? 我在这做错了什么? How can I get this icon to be displayed fully? 如何让这个图标完全显示? What's the standard icon size to be used for system tray? 系统托盘使用的标准图标大小是多少?

Edit: I am using AWT SystemTray and TrayIcon 编辑:我正在使用AWT SystemTray和TrayIcon

After you've retrieved the actual image resource from disk, you can resize it to the size you need by creating a "fake" one on-the-fly and taking its width. 从磁盘检索实际图像资源后,可以通过即时创建“假”并获取其宽度来将其调整为所需大小。

I found that this was better than using the setImageAutoSize(true) method, as that method does not scale the image smoothly at all. 我发现这比使用setImageAutoSize(true)方法更好,因为该方法根本不能平滑地缩放图像。

BufferedImage trayIconImage = ImageIO.read(getClass().getResource("/path/to/icon.png"));
int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
TrayIcon trayIcon = new TrayIcon(trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH));

To display the icon at an optimal size, you will need to manually resize it to the correct size. 要以最佳尺寸显示图标,您需要手动将其调整为正确的大小。 This correct size can differ between operating systems and preferences, so Java provides a method to acquire the task bar icon dimensions, which are 16x16 in the case of your example image. 这种正确的大小可能因操作系统和首选项而异,因此Java提供了一种获取任务栏图标尺寸的方法,对于示例图像,这些尺寸为16x16。

if (SystemTray.isSupported()) {
    SystemTray tray = SystemTray.getSystemTray();
    Dimension trayIconSize = tray.getTrayIconSize();
    // resize icon image to trayIconSize
    // create your tray icon off of the resized image
}

According to TrayIcon.setImageAutoSize(boolean) . 根据TrayIcon.setImageAutoSize(boolean)

Sets the auto-size property. 设置auto-size属性。 Auto-size determines whether the tray image is automatically sized to fit the space allocated for the image on the tray. 自动尺寸确定托盘图像是否自动调整大小以适合托盘上图像的分配空间 By default, the auto-size property is set to false. 默认情况下,auto-size属性设置为false。

If auto-size is false, and the image size doesn't match the tray icon space, the image is painted as-is inside that space — if larger than the allocated space, it will be cropped. 如果auto-size为false,并且图像大小与托盘图标空间不匹配,则图像将按原样绘制在该空间内 - 如果大于分配的空间, 则将对其进行裁剪。

I've ended up combining some of these answers to make the code I'm using. 我最终结合了一些这些答案来制作我正在使用的代码。

This is producing a good looking icon in my system tray from a png that starts at 100x100. 这是从我的系统托盘中以100x100开始的png生成一个漂亮的图标。

It's worth noting that on a retina MacBook the icon looks worse scaled down. 值得注意的是,在视网膜MacBook上,图标看起来更糟糕了。 So I do a check elsewhere to see if it's running on a mac and don't apply this if it is. 所以我在别处检查它是否在mac上运行,如果是的话就不应用它。

public Image imageForTray(SystemTray theTray){

    Image trayImage = Toolkit.getDefaultToolkit().getImage("my100x100icon.png");
    Dimension trayIconSize = theTray.getTrayIconSize();
    trayImage = trayImage.getScaledInstance(trayIconSize.width, trayIconSize.height, Image.SCALE_SMOOTH);

    return trayImage;
}

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

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