0

I have a Bitmap and I need to set some of its pixels to transparent. Although I'm creating the Bitmap with Config.ARGB_8888, when I call Bitmap.hasAlpha() on it, it returns false. I can't use Bitmap.setHasAlpha() because that was only added in API level 12 whereas my minimum supported API needs to be 9. How can I do this?

Catherine
  • 13,588
  • 9
  • 39
  • 60
  • Possible Duplicate: Check the answer here: http://stackoverflow.com/questions/16755551/alternative-to-setalpha-in-api-level-8 – Manu Jul 24 '13 at 19:24
  • 1
    That's a question about animations, not Bitmap manipulation! – Catherine Jul 24 '13 at 19:27

2 Answers2

0

Here you go:

Paint paint = new Paint();
paint.setAlpha(100);
canvas.drawBitmap(bitmap, src, dst, paint);

How to change a bitmap's opacity?

Edit:

//Create an array of pixels of the bitmap using Bitmap getPixels function
//Traverse through the pixels array and if that pixel matches your condition to change it to transparent
// Change the color of that pixel to transparent using Color.Transparent
//Finally set them back using Bitmap setPixels function

Example code:

import android.graphics.Color;

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),       myBitmap.getHeight());

//traverse through pixel array and if condition is met 
    pixels[i] = Color.TRANSPARENT;
myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

how to change the color of certain pixels in bitmap android

Community
  • 1
  • 1
Manu
  • 81
  • 1
  • 5
  • 1
    I need to set *some* of its pixels to transparent. Not the whole Bitmap. – Catherine Jul 24 '13 at 19:37
  • It doesn't work because my Bitmap does not have alpha because I can't call setAlpha on it. – Catherine Jul 24 '13 at 21:09
  • 1
    Erm, that would be setHasAlpha(). The problem is that when I set a pixel to transparent in a Bitmap for which hasAlpha() is false, it's just set to black. – Catherine Jul 24 '13 at 21:52
0
// Convert red to transparent in a bitmap
public static Bitmap makeAlpha(Bitmap bit) {
    int width =  bit.getWidth();
    int height = bit.getHeight();
    Bitmap myBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    int [] allpixels = new int [ myBitmap.getHeight()*myBitmap.getWidth()];
    bit.getPixels(allpixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(),myBitmap.getHeight());
    myBitmap.setPixels(allpixels, 0, width, 0, 0, width, height);
            return myBitmap;
      }

The above code is equivalent to setHasAlpha. Give it a Bitmap, and it will return a Bitmap where HasAlpha is true, and will therefore be able to have transparency. This works in API level 8. I have not tested it in anything lower.

Andrew Jenkins
  • 211
  • 3
  • 7
  • That's interesting, I will have to test it out! Will update once I've done so (although no longer relevant to me, may be useful to someone else...) – Catherine Jun 20 '14 at 04:15