开发者

How to crop a .png EncodedImage in BlackBerry while preserving transparency

I have a single .png image with several icons on it (with transpar开发者_JAVA百科ent areas) and would like to crop individual icons from it. In Java ME it was rather straight-forward, but in BlackBerry I haven't found an equivalent. The code here shows an example with a Bitmap, however doing so paints the transparent areas with white color:

public Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    Graphics g = new Graphics(result);
    g.drawBitmap(0, 0, width, height, image, x, y);
    return result;
}

I need the same for an EncodedImage to keep the transparency, but Graphics constructor accepts only a Bitmap. Is there any other way to accomplish this? Thank you for any tips.

UPDATE:

Transparency can be preserved if you omit the intermediate Graphics object altogether, and set the ARGB data directly to the newly created Bitmap, like so:

public Bitmap cropImage(Bitmap image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    int[] argbData = new int[width * height];
    image.getARGB(argbData, 0, width, x, y, width, height);
    result.setARGB(argbData, 0, width, 0, 0, width, height);
    return result;
}


Sorry I didn't try this code but it should give you the idea:

int[] argbData = new int[ width * height ];
image.getARGB(      argbData,
                    0,
                    width
                    x,
                    y,
                    width,
                    height);

Bitmap result = new Bitmap(width, height);
Graphics g = new Graphics(result);
g.drawARGB(argbData , 0, width, 0, 0, width, height);

return result;


try to use

g.setGlobalAlpha(0);

before

g.drawBitmap(0, 0, width, height, image, x, y);

or you can use

drawARGB(int[] data, int offset, int scanLength, int x, int y, int width, int height) 

which preserve the alpha in the destination image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜