A faster way to set the color of a bitmap (PNG) instead of pixel by pixel

I have some png files that I apply color to. Color changes according to user choice. I change the color according to 3 RGB values ​​set using a different method. PNG files are random form with full transparency outside the form. I do not want to change the transparency, only the value of RGB. I am currently setting the RGB values ​​per pixel (see code below).

I realized that this is incredibly slow and perhaps not efficient enough in the application. Is there a better way to do this?

This is what I am doing right now. You can see that the pixel array is huge for an image that occupies a decent part of the screen:

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; } 
+1
source share
2 answers

Have you looked at the features available in Bitmap ? Something like extractAlpha sounds like it might be useful. You also look at how such features are implemented in Android to find out how you can adapt them to your specific case if this does not fit your needs.

+2
source

The answer that worked for me is to write a square here Transparent jpegs

They provide a faster piece of code to perform this specific task. I tried extractAlpha and it did not work, but a square solution. Just change their solution, change the color bit instead of the alpha bit instead.

i.e.

 pixels[x] = (pixels[x] & 0xFF000000) | (color & 0x00FFFFFF); 
+1
source

All Articles