我有一个复合材料,并希望使用png文件作为背景图像。我可以这样做,问题是当图像使用透明度时它不起作用而是显示白色。有关如何使其发挥作用的任何想法?
谢谢!
答案 0 :(得分:3)
答案 1 :(得分:1)
解决了!我已经解决了这个问题 - 显示包含透明区域的PNG文件。我无法使用标签(实际上article quoted状态“标签不支持原生透明度”)所以我将图像直接放入画布中。成功的另一个关键是使用包含第三个参数的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);