Codenameone 代号:来自internet源的圆形图像

Codenameone 代号:来自internet源的圆形图像,codenameone,parparvm,Codenameone,Parparvm,我试图显示一个圆形的图像,我直接从互联网上获得。 我使用下面的代码创建了一个圆形遮罩,从互联网上获取图像,然后尝试在图像或标签上设置遮罩。这些方法都不起作用。如果移除遮罩,图像将显示良好。如果我保留设置掩码的代码,那么我看到的只是一个空的白色圆圈 我的想法是,如果我在图像本身上应用了掩码,那么它可能不会生效,因为在应用掩码时图像没有下载 但我似乎不明白为什么在标签上调用setMask也不起作用 // Create MASK Image maskImage = Image.crea

我试图显示一个圆形的图像,我直接从互联网上获得。 我使用下面的代码创建了一个圆形遮罩,从互联网上获取图像,然后尝试在图像或标签上设置遮罩。这些方法都不起作用。如果移除遮罩,图像将显示良好。如果我保留设置掩码的代码,那么我看到的只是一个空的白色圆圈

我的想法是,如果我在图像本身上应用了掩码,那么它可能不会生效,因为在应用掩码时图像没有下载

但我似乎不明白为什么在标签上调用
setMask
也不起作用

   // Create MASK

    Image maskImage = Image.createImage(w, l);
    Graphics g = maskImage.getGraphics();
    g.setAntiAliased(true);
    g.setColor(0x000000);
    g.fillRect(0, 0, w, l);
    g.setColor(0xffffff);
    g.fillArc(0, 0, w, l, 0, 360);
    Object mask = maskImage.createMask();



    // GET IMAGE
    com.cloudinary.Cloudinary cloudinary = new com.cloudinary.Cloudinary(ObjectUtils.asMap(
            "cloud_name", "REMOVED",
            "api_key", "REMOVED",
            "api_secret", "REMOVED"));
    // Disable private CDN URLs as this doesn't seem to work with free accounts
    cloudinary.config.privateCdn = false;
    Image placeholder = Image.createImage(150, 150);
    EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);
    Image img2 = cloudinary.url()
            .type("fetch") // Says we are fetching an image
            .format("jpg") // We want it to be a jpg
            .transformation(
                    new Transformation()
                    .radius("max").width(150).height(150).crop("thumb").gravity("faces").image(encImage, "http://upload.wikimedia.org/wikipedia/commons/4/46/Jennifer_Lawrence_at_the_83rd_Academy_Awards.jpg");

    Label label = new Label(img2);
    label.setMask(mask);  // also tried to do img2.applyMask(mask); before passing img2 

我认为您设置到标签的制作代码可能与您从Cloudinary获得的屏蔽代码冲突。

因此我尝试了各种方法:

1) 移除通过cloudinary设置的掩码-这不起作用

2) 将遮罩应用于占位符和编码图像(正如预期的那样,这些不应影响即将发布的最终版本)

3) 这才是有效的!我不确定问题是否真的是在应用遮罩之前或之后下载图片。。时间可以告诉我们未来的路

    Label label = new Label();
    img2.applyMask(mask);  // If you remove this line , the image will no longer be displayed, I will only see a rounded white circle ! I am not sure what this is doing, it might be simply stalling the process until the image is downloaded? or maybe somehow calling repaint or revalidate
    label.setIcon( img2.applyMask(mask));
如果其他人有类似的问题,以下是对我有效的方法:

        //CREATE MASK
    Image maskImage = Image.createImage(w, l);
    Graphics g = maskImage.getGraphics();
    g.setAntiAliased(true);
    g.setColor(0x000000);
    g.fillRect(0, 0, w, l);
    g.setColor(0xffffff);
    g.fillArc(0, 0, w, l, 0, 360);
    Object mask = maskImage.createMask();

    //CONNECT TO CLOUDINARY 
    com.cloudinary.Cloudinary cloudinary = new com.cloudinary.Cloudinary(ObjectUtils.asMap(
            "cloud_name", "REMOVED",
            "api_key", "REMOVED",
            "api_secret", "REMOVED"));
    // Disable private CDN URLs as this doesn't seem to work with free accounts
    cloudinary.config.privateCdn = false;

    //CREATE IMAGE PLACEHOLDERS 
    Image placeholder = Image.createImage(w, l);
    EncodedImage encImage = EncodedImage.createFromImage(placeholder, false);

    //DOWNLOAD IMAGE
    Image img2 = cloudinary.url()
            .type("fetch") // Says we are fetching an image
            .format("jpg") // We want it to be a jpg
            .transformation(
                    new Transformation()
                    .crop("thumb").gravity("faces")
                    .image(encImage, url);


    // Add the image to a label and place it on the form.
    //GetCircleImage(img2);
    Label label = new Label();
    img2.applyMask(mask);   // If you remove this line , the image will no longer be displayed, I will only see a rounded white circle ! I am not sure what this is doing, it might be simply stalling the process until the image is downloaded? or maybe somehow calling repaint or revalidate
    label.setIcon( img2.applyMask(mask));

谢,我真的很感谢你抽出时间!!非常感谢你。如果以后它给我带来任何其他问题,我将不得不深入研究它,但现在它似乎一直有效

Cloudinary API返回一个URLImage,该URLImage与Label.setMask()方法不兼容,因为从技术上讲,URLImage是一个动画图像(在加载完成之前它是一个占位符图像,然后“动画化”成为目标图像)

我刚刚发布了cloudinary cn1lib的一部分,它为您提供了一些解决此问题的选项

我添加了两个新的
image()
方法。在将其设置为标签图标之前,可使用
ImageAdapter
参数将遮罩应用于图像本身。那么就根本不用Label.setMask()了

另一种方法使用下面新的异步映像加载API异步加载映像。您在回调中收到的图像是“真实”图像,因此您可以将其与遮罩一起正常使用


如果您试图添加“动画”图像并对其进行遮罩以使其更清晰,我们将在Label.setMask()和setIcon()方法中添加一个软警告。

这是有意义的。我的印象是,这可能是一个图像下载问题。我将执行你的建议。我认为这是前进的正确道路。谢谢