3

我有一个 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;
}
4

2 回答 2

2

据我从handleEvent(...)方法中可以看出,您忘记处理旧图像。

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);
        Image old = lblCoverfront.getImage();
        lblCoverfront.setImage(Helper.ImageScale(buffer_Coverfront, width, height));
        buffer_Coverfront.dispose();
        buffer_Coverfront = null;

        if (old != null) old.dispose();

    } // handleEvent
}); // Listener

上面的代码假设控件的图像在这个方法中设置。如果您还从其他地方设置初始图像,则必须改进处置条件......

于 2013-05-02T15:56:08.223 回答
2

由于我无法在评论中发布代码,所以它是:

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;

                String filename_front = my_collection.getGlobal_path() + "Cover/" + selected_movie.getCover_front();

                try {
                    Image image = new Image(Display.getDefault(), filename_front);
                    Image scaled = Helper.ImageScale(image, width, height);
                    lblCoverfront.setImage(scaled);
                    image.dispose();
                    scaled.dispose();

                } catch (Exception e) {
                } // end try

        } // handleEvent
    }); // Listener

再次感谢 Tonny Madsen 提供的有用提示。

于 2013-05-03T19:21:41.853 回答