简体   繁体   English

在SWT上具有透明度的PNG图像

[英]PNG image with transparency on SWT

I've got a Composite and would like to use a png file as background image. 我有一个复合材料,并希望使用png文件作为背景图像。 I can do that, the problem is when the image uses transparency it doesn't work and shows a white colour instead. 我可以这样做,问题是当图像使用透明度时它不起作用而是显示白色。 Any thoughts on how to get this to work? 有关如何使其工作的任何想法?

Thanks! 谢谢!

Does this article help? 这篇文章有帮助吗?

Taking a look at SWT Images 看看SWT图像

It talks about drawing an image (albeit a GIF) to a Canvas with transparency ( Canvas extends Composite ). 它讨论了将图像(尽管是GIF)绘制到具有透明度的CanvasCanvas扩展Composite )。

SOLVED! 解决了! I've solved exactly this problem - displaying PNG files that contain transparent areas. 我已经解决了这个问题 - 显示包含透明区域的PNG文件。 I could not get it to work with Labels (in fact the article quoted states "Label does not support native transparency") so I put the image directly into a canvas instead. 我无法使用标签(事实上文章引用了 “标签不支持原生透明度”),所以我将图像直接放入画布中。 The other key to success is to use the Image constructor that includes a 3rd parameter, which is the transparency mask. 成功的另一个关键是使用包含第三个参数的Image构造函数,该参数是透明度蒙版。 An extra step I did is to call setRegion, which means that mouse events (such as mouse clicks) only fire when they occur over visible pixels. 我做的另一个步骤是调用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);

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

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