我有一些png文件正在应用颜色。颜色根据用户选择而改变。我通过另一种方法设置的3个RGB值更改颜色。 png文件是一个随机形状,在形状之外具有完全透明度。我不想修改透明度,只有RGB值。目前,我正在逐个像素地设置RGB值(请参阅下面的代码)。设置一个(PNG)位图颜色而不是像素的更快方法
我已经意识到这是非常慢,可能只是没有足够的效率在应用程序中。有没有更好的方法可以做到这一点?
这是我目前正在做的。你可以看到,像素阵列是巨大的,占用了屏幕的体面部分的图像:
public void foo(Component component, ComponentColor compColor, int userColor) {
int h = component.getImages().getHeight();
int w = component.getImages().getWidth();
mBitmap = component.getImages().createScaledBitmap(component.getImages(), w, h, true);
int[] pixels = new int[h * w];
//Get all the pixels from the image
mBitmap[index].getPixels(pixels, 0, w, 0, 0, w, h);
//Modify the pixel array to the color the user selected
pixels = changeColor(compColor, pixels);
//Set the image to use the new pixel array
mBitmap[index].setPixels(pixels, 0, w, 0, 0, w, h);
}
public int[] changeColor(ComponentColor compColor, int[] pixels) {
int red = compColor.getRed();
int green = compColor.getGreen();
int blue = compColor.getBlue();
int alpha;
for (int i=0; i < pixels.length; i++) {
alpha = Color.alpha(pixels[i]);
if (alpha != 0) {
pixels[i] = Color.argb(alpha, red, green, blue);
}
}
return pixels;
}
也许你可以使用一个图像与一个调色板?由于所有内容都使用一种颜色,这意味着无论如何,只能从一个输入中获得256种可能的像素颜色(因为RGB部分将是相同的,并且只有A部分会有所不同)。如果您的图像具有256种不同颜色的调色板,而这些颜色只与alpha通道的值不同,则只需修改调色板,而不是图像数据本身。 – 2010-11-22 23:06:49
我要么不理解你,要么你不了解我的问题。我想修改的每个图像都有2个alpha值,完全透明和半透明/不透明。我想修改RGB值并保持alpha值相同。这就是说,虽然我得到调色板的概念,但在android中没有Palette类。你建议我读什么? – user432209 2010-11-22 23:32:06