我有一些png文件,我正在应用颜色。颜色根据用户的选择而改变。我通过从另一个方法设置的3个RGB值来更改颜色。png文件是一个完全透明的随机形状。我不想修改透明度,只想修改RGB值。目前,我正在逐像素设置RGB值(见下面的代码)。
我逐渐意识到这是难以置信的缓慢,可能只是在应用程序中没有足够的效率。有更好的办法吗?
以下是我目前正在做的事情。你可以看到像素阵列对于占据屏幕一部分的图像来说是巨大的:
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;
}