I have a single .png image with several icons on it (with transparent 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;
}