首页 > 解决方案 > 我可以在代号 One 的 Dialog.show 中插入图像吗

问题描述

我可以在 Dialog.show 中插入图像吗?如果是的话,有人可以提供一个例子吗?这就是我想出的:

  img.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent evt) {
                    Dialog.show("Image:", images(value.toString()), new Command("Cancel"));
                }
            });

   public ImageViewer images(String id) {
    int id2 = Integer.parseInt(id);
    String img = ServiceTask.getInstance().ReturnImage(id2);
    Image placeholder = Image.createImage(getWidth() / 3 - 4, getWidth() / 3 - 4, 0xbfc9d2);
    EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
    ImageViewer img1 = new ImageViewer(URLImage.createToStorage(encImage, "file" + img,
            "http://localhost/projectname/web/images/" + img));

    return img1;
}

标签: javacodenameone

解决方案


在您的代码中,您使用了ImageViewer: 我不确定它是否适合在Dialog. 但是请注意:如果您想要最大分辨率的照片ImageViewerURLImage则不适合一起使用,因为URLImage总是调整图像大小。

让我们从一个显示图像的简单对话框示例开始:

Form hi = new Form("Hi World", BoxLayout.y());
        Button btn = new Button("Tap me to show Dialog");
        hi.add(btn);
        hi.show();

        btn.addActionListener(l -> {
            Dialog dialog = new Dialog("Example", BoxLayout.y());
            dialog.add(new SpanLabel("Example of Dialog including an image"));
            dialog.add(new Label(FontImage.createMaterial(FontImage.MATERIAL_RADIO, "Label", 7.0f)));
            dialog.setDisposeWhenPointerOutOfBounds(true);
            dialog.show();
        });

结果:

在此处输入图像描述

如果您需要URLImage,就像在您的代码中一样,您可以使用它:

Form hi = new Form("Hi World", BoxLayout.y());
        Button btn = new Button("Tap me to show Dialog");
        hi.add(btn);
        hi.show();

        btn.addActionListener(l -> {
            Dialog dialog = new Dialog("Example", BoxLayout.y());
            dialog.add(new SpanLabel("Example of Dialog including an image"));
            EncodedImage placeholder = EncodedImage.createFromImage(Image.createImage(CN.convertToPixels(30), CN.convertToPixels(30), 0x00ffffff), true);
            URLImage meditationIcon = URLImage.createToStorage(placeholder, "gnu_meditation_levitation_small.png",
                    "https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", URLImage.RESIZE_SCALE_TO_FILL);
            meditationIcon.fetch();
            dialog.add(FlowLayout.encloseCenter(new Label(meditationIcon)));
            dialog.setDisposeWhenPointerOutOfBounds(true);
            dialog.show();
        });

结果:

在此处输入图像描述

但是,在我看来,当Dialog显示图像时,应该立即显示图像,因此URLImage在这种情况下不是一个好的选择。在类似的情况下,我更喜欢预下载图像,如以下代码所示。结果是一样的,但是显示时图像已经下载Dialog(否则不显示):

Form hi = new Form("Hi World", BoxLayout.y());
        Button btn = new Button("Tap me to show Dialog");
        hi.add(btn);
        hi.show();

        AsyncResource<Image> myImage = Util.downloadImageToStorage("https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", "myImage.png");

        btn.addActionListener(l -> {
            Dialog dialog = new Dialog("Example", BoxLayout.y());
            dialog.add(new SpanLabel("Example of Dialog including an image"));
            try {
                // Very small timeout (10 ms), if the Image wasn't pre-downloaded it will not be shown
                dialog.add(FlowLayout.encloseCenter(new Label(myImage.get(10).scaledWidth(CN.convertToPixels(30)))));
            } catch (InterruptedException ex) {
                Log.e(ex);
            }
            dialog.setDisposeWhenPointerOutOfBounds(true);
            dialog.show();
        });

您还可以添加“取消”按钮,例如在您的代码中:

        Form hi = new Form("Hi World", BoxLayout.y());
        Button btn = new Button("Tap me to show Dialog");
        hi.add(btn);
        hi.show();

        AsyncResource<Image> myImage = Util.downloadImageToStorage("https://www.informatica-libera.net/immagini/gnu_meditation_levitation_small.png", "myImage.png");

        btn.addActionListener(l -> {
            Dialog dialog = new Dialog("Example", BoxLayout.y());
            dialog.add(new SpanLabel("Example of Dialog including an image"));
            try {
                // Very small timeout (10 ms), if the Image wasn't pre-downloaded it will not be shown
                dialog.add(FlowLayout.encloseCenter(new Label(myImage.get(10).scaledWidth(CN.convertToPixels(30)))));
            } catch (InterruptedException ex) {
                Log.e(ex);
            }
            Button cancel = new Button("Cancel");
            cancel.addActionListener(ll -> dialog.dispose());
            dialog.add(cancel);
            dialog.show();
        });

您可以Dialog根据自己的喜好设置样式,CSS 是一个不错的选择。


推荐阅读