【发布时间】:2020-10-24 16:25:52
【问题描述】:
我正在尝试使图像适合可随窗口调整大小的复合材料(或画布)。当它出现时,我想确保它不会弄乱纵横比,所以我编写了这段代码来覆盖打印功能:
bazaarMenu.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
Image image = SWTResourceManager.getImage(mainFrame.class, "/assets/SkyHub/resources/images/bazaarBackground.jpg");//get the image
int w = bazaarMenu.getClientArea().width;//composite width
int h = bazaarMenu.getClientArea().height;//composite height
int iw = image.getBounds().width;//image width
int ih = image.getBounds().height;// image height
//these are the values of the width/height of the image if ratio is same with the
//composite's
int w1 = ih * (w / h);
int h1 = iw * (h / w);
//Find which value can be used and draw the image
if(w1 <= w)
{
event.gc.drawImage(image, 0, 0, w1, ih, 0, 0, w, h);
}
else// if(w / h < 76 / 45)
{
event.gc.drawImage(image, 0, 0, iw, h1, 0, 0, w, h);
}
}
});
但是,我无法得到我想要的结果。可以看到图像拉伸或仅可以看到其中的一部分。有没有其他方法可以做,或者我错过了什么。
【问题讨论】:
标签: java image swt aspect-ratio