代码之家  ›  专栏  ›  技术社区  ›  adrian.tarau

尽快清除透明缓冲区图像

  •  10
  • adrian.tarau  · 技术社区  · 16 年前

    我使用以下代码创建了一个透明的BuffereImage(我认为它与创建方式无关):

                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。

    2 回复  |  直到 16 年前
        1
  •  20
  •   adrian.tarau    16 年前

    明白了:)用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());
    
        2
  •  9
  •   SyntaxT3rr0r    16 年前

    一个相对快速的方法,但我不知道它是否是最快的(我希望看到其他答案),就是有另一张图片,你永远不会修改,并且总是“完全清除”/“完全透明”,然后你做一个光栅复制,比如说你将该复制命名为“清除”:

    imageYouWantToClear.setData( CLEAR.getRaster() );
    

    请注意,当涉及性能时,使用图形可能会非常棘手,因为存在许多没有很好记录的行为。例如,您的图像(说清楚的一个) 也许 setRgb() )事实证明,很难意识到您刚刚失去了硬件加速的好处。

    顺便说一句,请确保您的两个BuffereImage都使用“兼容”模式:在Windows上键入\u INT\u ARGB可能没有问题,但在OS X上则没有问题。因此,您希望通过以下方式创建它们:

    GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
    

    推荐文章