如何裁剪.png格式恩codeDIMAGE黑莓,而preserving透明度 [英] How to crop a .png EncodedImage in BlackBerry while preserving transparency
问题描述
我有与它的几个图标,一个PNG图片(透明区域),并想从中裁剪单个图标。在Java ME这是相当直接的,但在黑莓手机我还没有找到一个等效。该 $ C $其中,C显示了一个位图的一个例子,但是这样做有白色油漆透明区域颜色:
公共位图cropImage(位图图像,诠释的x,INT Y,INT宽度,高度INT){
位图的结果=新位图(宽,高);
图形G =新的图形(结果);
g.drawBitmap(0,0,宽度,高度,图像,X,Y);
返回结果;
}
我需要一间codeDIMAGE同样保持透明度,但图形
的构造函数只接受位图
。是否有任何其他的方式来做到这一点?感谢您的任何提示。
更新:
透明度可以preserved如果省略中间的图形对象干脆,直接设置ARGB数据到新创建的位图,就像这样:
公共位图cropImage(位图图像,诠释的x,INT Y,INT宽度,高度INT){
位图的结果=新位图(宽,高);
INT [] = argbData新INT [宽*高]。
image.getARGB(argbData,0,宽度,X,Y,宽度,高度);
result.setARGB(argbData,0,宽度,0,0,宽度,高度);
返回结果;
}
对不起,我没有尝试这个code,但它应该给你的理念是:
INT [] = argbData新INT [宽*高]。
image.getARGB(argbData,
0,
宽度
X,
Y,
宽度,
高度);位图的结果=新位图(宽,高);
图形G =新的图形(结果);
g.drawARGB(argbData,0,宽度,0,0,宽度,高度);返回结果;
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;
}
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;
这篇关于如何裁剪.png格式恩codeDIMAGE黑莓,而preserving透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!