我有一个使用以下代码创建的透明 BufferedImage(我认为它的创建方式不相关):
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gs.getDefaultConfiguration();
Rectangle screen = transformationContext.getScreen();
// Create an image that supports transparent pixels
return gc.createCompatibleImage((int) screen.getWidth(), (int) screen.getHeight(),
Transparency.BITMASK);
如何在不重新创建图像的情况下以最快的方式清除图像(与创建状态相同的空图像)?重新创建图像会给 GC 带来负担,暂停 VM 并卡住 UI。
最佳答案
知道了 :) 使用 clearRect 而不是填充透明颜色。
graphics = (Graphics2D) offlineBuffer.getGraphics();
graphics.setBackground(new Color(255, 255, 255, 0));
Rectangle screen = transformationContext.getScreen();
graphics.clearRect(0,0, (int)screen.getWidth(), (int)screen.getHeight());
关于java - 尽可能快地清除透明的 BufferedImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2367365/