onLoad 在居中的 PopupPanel 中显示 GWT 图像

发布于 2024-10-20 23:17:09 字数 1013 浏览 3 评论 0原文

使用 GWT,我使用 ClickHandler 显示图像缩略图,然后在居中的 PopupPanel 中显示完整图像(可以是几 MB)。为了使其居中,必须在显示弹出窗口之前加载图像,否则图像的左上角将放置在屏幕的中间(图像认为它是 1px 大)。这是我用来执行此操作的代码:

    private void showImagePopup() {
        final PopupPanel popupImage = new PopupPanel();
        popupImage.setAutoHideEnabled(true);
        popupImage.setStyleName("popupImage"); /* Make image fill 90% of screen */

        final Image image = new Image();
        image.addLoadHandler(new LoadHandler() {
            @Override
            public void onLoad(LoadEvent event) {
                popupImage.add(image);
                popupImage.center();
            }
        });
        image.setUrl(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
        Image.prefetch(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
    }

但是,LoadEvent 事件永远不会被触发,因此图像永远不会显示。我怎样才能克服这个问题?我想避免使用 http://code.google.com/p/gwt-image -loader/ 因为如果我能避免的话我不想添加额外的库。谢谢。

Using GWT I am displaying an image thumbnail with a ClickHandler that then shows the full image (can be several MB) in a centered PopupPanel. In order to have it centered the image must be loaded before the popup is shown, otherwise the top-left corner of the image is placed in the middle of the screen (the image thinks it is 1px large). This is the code I am using to do this:

    private void showImagePopup() {
        final PopupPanel popupImage = new PopupPanel();
        popupImage.setAutoHideEnabled(true);
        popupImage.setStyleName("popupImage"); /* Make image fill 90% of screen */

        final Image image = new Image();
        image.addLoadHandler(new LoadHandler() {
            @Override
            public void onLoad(LoadEvent event) {
                popupImage.add(image);
                popupImage.center();
            }
        });
        image.setUrl(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
        Image.prefetch(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
    }

However, the LoadEvent event is never fired, and thus the image is never shown. How can I overcome this? I want to avoid using http://code.google.com/p/gwt-image-loader/ because I do not want to add extra libraries if I can avoid it at all. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬﹪忆之殇 2024-10-27 23:17:09

onLoad() 方法仅在图像加载到 DOM 后才会触发。这是一个快速解决方法:

...

final Image image = new Image(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
image.addLoadHandler(new LoadHandler() {
    @Override
    public void onLoad(LoadEvent event) {
        // since the image has been loaded, the dimensions are known
        popupImage.center(); 
        // only now show the image
        popupImage.setVisible(true);
    }
 });

 popupImage.add(image);
 // hide the image until it has been fetched
 popupImage.setVisible(false);
 // this causes the image to be loaded into the DOM
 popupImage.show();

 ...

希望有帮助。

The onLoad() method will only fire once the image has been loaded into the DOM. Here is a quick workaround:

...

final Image image = new Image(attachmentUrl + CFeedPostAttachment.ATTACHMENT_FILE);
image.addLoadHandler(new LoadHandler() {
    @Override
    public void onLoad(LoadEvent event) {
        // since the image has been loaded, the dimensions are known
        popupImage.center(); 
        // only now show the image
        popupImage.setVisible(true);
    }
 });

 popupImage.add(image);
 // hide the image until it has been fetched
 popupImage.setVisible(false);
 // this causes the image to be loaded into the DOM
 popupImage.show();

 ...

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文