我有一些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;
}