1

GWT を使用して、中央の PopupPanel に完全な画像 (数 MB になる場合があります) を表示する ClickHandler で画像のサムネイルを表示しています。画像を中央に配置するには、ポップアップが表示される前に画像をロードする必要があります。そうしないと、画像の左上隅が画面の中央に配置されます (画像は 1 ピクセルの大きさと見なされます)。これは私がこれを行うために使用しているコードです:

    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/の使用を避けたいのは、回避できるのであれば余分なライブラリを追加したくないからです。ありがとう。

4

1 に答える 1

3

この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();

 ...

お役に立てば幸いです。

于 2011-03-10T12:12:47.123 に答える