我有一个 java swt 应用程序。在容器的 resize 方法中,我想调整图像的大小,使用以下代码显示。然而似乎 - 即使我处理所有东西(真的吗?)内存消耗一直在增加......我找不到我的错误。为什么以及在哪里这段代码会吃掉我所有的记忆?
这是调整大小的侦听器:
tabCover.addListener(SWT.Resize, new Listener() {
public void handleEvent(Event event) {
// Set images: cover_front
int width = tabCover.getSize().x - 30;
int height = tabCover.getSize().y - 30;
Image buffer_Coverfront;
buffer_Coverfront = new Image(Display.getDefault(), filename);
lblCoverfront.setImage(Helper.ImageScale(buffer_Coverfront, width, height));
buffer_Coverfront.dispose();
buffer_Coverfront = null;
} // handleEvent
}); // Listener
这里是缩放功能:
public static Image ImageScale(Image image, int width, int height) {
ImageData data = image.getImageData();
// Some logic to keep the aspect ratio
float img_height = data.height;
float img_width = data.width;
float container_height = height;
float container_width = width;
float dest_height_f = container_height;
float factor = img_height / dest_height_f;
int dest_width = (int) Math.floor(img_width / factor );
int dest_height = (int) dest_height_f;
if(dest_width > container_width) {
dest_width = (int) container_width;
factor = img_width / dest_width;
dest_height = (int) Math.floor(img_height / factor);
}
// Image resize
data = data.scaledTo(dest_width, dest_height);
Image scaled = new Image(Display.getDefault(), data);
image.dispose();
return scaled;
}