我会显示具有透明度的.png图像,默认情况下效果很好...直到我调整图像大小为止:
public Image resize(Image image, int width, int height)
{
Image scaled = new Image(Display.getDefault(), width, height);
scaled.getImageData().transparentPixel = image.getImageData().transparentPixel;
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(image, 0, 0,image.getBounds().width, image.getBounds().height, 0, 0, width, height);
gc.dispose();
return scaled;
}
然后透明度消失了,我可以看到“白色”颜色
getImageData()
为您提供了图像数据的副本,因此在其上设置透明像素不会更改原始图像。
相反,您需要使用透明像素集从缩放后的图像数据中创建另一个图像。所以像这样:
Image scaled = new Image(Display.getDefault(), width, height);
GC gc = new GC(scaled);
gc.setAntialias(SWT.ON);
gc.setInterpolation(SWT.HIGH);
gc.drawImage(image, 0, 0,image.getBounds().width, image.getBounds().height, 0, 0, width, height);
gc.dispose();
// Image data from scaled image and transparent pixel from original
ImageData imageData = scaled.getImageData();
imageData.transparentPixel = image.getImageData().transparentPixel;
// Final scaled transparent image
Image finalImage = new Image(Display.getDefault(), imageData);
scaled.dispose();
请注意,这仅在原始图像数据具有与缩放图像数据(尤其是调色板数据)相同的格式时才有效
如果数据采用其他格式,请使用:
ImageData origData = image.getImageData();
imageData.transparentPixel = imageData.palette.getPixel(origData.palette.getRGB(origData.transparentPixel));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句