我有一个单独的.png图像,上面有多个图标(带有透明区域),并且想从中裁剪单个图标。在Java ME中它很直接,但在黑莓中我还没有找到相同的东西。该code here shows an example with a Bitmap,但是这样做油漆的透明区域使用白色:如何在保留透明度的情况下在BlackBerry中裁剪.png EncodedImage
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;
}
我需要同样的一个EncodedImage保持透明度,但Graphics
构造函数只接受一个Bitmap
。有没有其他方法可以实现这一点?感谢您提供任何提示。
UPDATE:
,如果你省略了中间的图形完全反对,并直接设置ARGB数据到新创建的位图,像这样透明度可保留:
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;
}
显然没有办法返回一个裁剪后的图像(从大图),同时保持透明度。我通过将图形上下文传递到我的方法中,并从较大的图像中直接将一个子集绘制到图形上下文中来解决这个问题 - 这样至少保留了透明度。 – Levon 2010-09-24 19:38:28