SWT上使用透明度的PNG图像

4

我有一个组合对象,想使用png文件作为背景图像。我可以做到这一点,但问题是当图像使用透明度时,它不起作用并显示白色。你们有什么想法如何让它正常工作?

谢谢!

2个回答

3

这篇文章是否有帮助?

浏览SWT图像

它讲述了如何在具有透明度的Canvas上绘制图像(尽管是GIF格式的图像),CanvasComposite的扩展。


1

问题已解决!我已经解决了这个问题——显示包含透明区域的PNG文件。我无法通过标签使其工作(事实上引用文章中指出“标签不支持本地透明度”),因此我将图像直接放入画布中。成功的另一个关键是使用包括第三个参数(即透明度掩码)的Image构造函数。我所做的额外步骤是调用setRegion,这意味着鼠标事件(如鼠标单击)仅在发生在可见像素上时触发。

ImageData id = new ImageData("basket.png");
Image image = new Image (display, id, id); //3rd parameter is transparency mask
Canvas c = new Canvas (shell, SWT.TRANSPARENT);
c.addPaintListener(
    new PaintListener(){
        public void paintControl(PaintEvent e) 
        {
            e.gc.drawImage(image, 0, 0);
        }
    }
);

//the image has been created, with transparent regions. Now set the active region
//so that mouse click (enter, exit etc) events only fire when they occur over
//visible pixels. If you're not worried about this ignore the code that follows
Region region = new Region();
Rectangle pixel = new Rectangle(0, 0, 1, 1);
for (int y = 0; y < id.height; y++)
{
    for (int x = 0; x < id.width; x++)
    {
        if (id.getAlpha(x,y) > 0)
        {
            pixel.x = id.x + x;
            pixel.y = id.y + y;
            region.add(pixel);
        }
    }
}
c.setRegion(region);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接