2010-09-24 72 views
1

我有一个单独的.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; 
} 
+0

显然没有办法返回一个裁剪后的图像(从大图),同时保持透明度。我通过将图形上下文传递到我的方法中,并从较大的图像中直接将一个子集绘制到图形上下文中来解决这个问题 - 这样至少保留了透明度。 – Levon 2010-09-24 19:38:28

回答

1

对不起,我没有尝试这个代码,但它应该给你的想法:

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; 
+0

谢谢你的代码,但结果是完全相同的 - 它不保留透明度,并在透明区域应该绘制白色像素。就目前而言,我仍然没有找到一种方法来从较大的图像中裁剪图像,同时保留透明部分。 – Levon 2010-09-24 18:54:05

+0

对不起,我忍不住,但你可以检查这个http://supportforums.blackberry.com/t5/Java-Development/Transparent-background-for-Bitmap/mp/233113/message-uid/233113 – 2010-09-24 21:39:27

+2

而不是创建一个Graphics对象,你可以直接在新的Bitmap中做一个result.setARGB(...)吗? – 2010-09-25 04:12:29

0

尝试使用

g.setGlobalAlpha(0); 

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

,或者您可以使用

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

其保存在目标图像中的阿尔法。

+0

谢谢,但请您提供一个如何在drawARGB()中获得int []数据的例子。谢谢! – Levon 2010-09-24 17:11:21

+0

另外,使用g.setGlobalAlpha(0)不起作用 - 它用纯白色绘制整个区域。 – Levon 2010-09-24 17:41:13