Workaround for applying a mask to an image in Codename One

时间:2016-10-20 19:37:28

标签: codenameone

I have a function that accepts an image and rounds it, like so:

    public static Image roundImage(Image img) {
    int width = img.getWidth();
    Image roundMask = Image.createImage(img.getWidth(), img.getHeight(), 0xff000000);
    Graphics gr = roundMask.getGraphics();
    gr.setColor(0xffffff);
    gr.fillArc(0, 0, width, width, 0, 360);
    Object mask = roundMask.createMask();
    img = img.applyMask(mask);
    return img;
}

This works great for images, but if I pass FontImage to it, the function throws an exception: java.lang.RuntimeException: Unsupported Operation. How can I check whether masking is a supported operation? I want to avoid changing the application logic.

1 个答案:

答案 0 :(得分:1)

FontImagerequiresDrawImage,是唯一的。您可以分别使用toImage()toEncodedImage()将其转换为常规图像,甚至转换为编码图像。

对于此用例,常规图像会更好/更快,因此toImage()应该效果最佳。