1

I've got a Bitmap with some transparent pixels and rest is mainly black (some black pixels possibly have a few sem-transparent pixels).

I need to re-use these bitmaps and want to be able to essentially create a Mask out of this bitmap at runtime and then try and blend with a block of another color (like Red, green etc) so that the end result is the same image but with red color (and those pixels which were semi-transparent black pixels turn into semi-transparent red pixels).

I've tried all sorts of color filters and xfermodes but have not been able to figure out. Please help!

strange
  • 9,654
  • 6
  • 33
  • 47
  • related question http://stackoverflow.com/questions/9856421/java-how-to-tint-this-png-programmatically/9856551#9856551 – Ozzy Apr 14 '12 at 23:46

2 Answers2

2

If you doesn't need high speed, you can use simple solution by manually blend pixels.

final Bitmap bmp = /* there your bitmap */;

int w = bmp.getWidth();
int h = bmp.getHeight();

for (int x = 0; x < w; x++) {
  for (int y = 0; y < h; y++) {
    int color = bmp.getPixel(x, y);

    // Shift your alpha component value to the red component's.
    color = (color << 24) & 0xFF000000;

    bmp.setPixel(x, y, color);
  }
}

If you need more effective processing, you must use (at least) getPixels method or, more preferable, native processing.

pepyakin
  • 2,217
  • 19
  • 31
0
 public void changeColor(Bitmap myBitmap) {

        int [] allpixels = new int [myBitmap.getHeight()*myBitmap.getWidth()];

        myBitmap.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

        for(int i = 0; i < allpixels.length; i++)
        {
            if(allpixels[i] == Color.BLACK)
            {
                allpixels[i] = Color.RED;
            }
        }

        myBitmap.setPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

    }
Andrew M.
  • 9
  • 1
  • 4
shakil.k
  • 1,623
  • 5
  • 17
  • 27