4

I wanted to mask a Bitmap with a black and white alpha mask. My mask image is black and white, the BLACK area means TRANSPARENT and WHITE area means OPAQUE.

What I need is: When I use this mask image to mask any other image, the area of resultant image should TRANSPARENT if the corresponding area of mask image is BLACK. Otherwise the area of resultant image should be OPAQUE.

I have attached the sample images. Please help me with this guys.

Sample Image: Sample Image for Masking

What I have tried so far: The following methods work fine. But they are very slow. I needed some solution that is efficient in terrms of speed and memory than these methods.

  • First Method:

      int width = rgbDrawable.getWidth();
      int height = rgbDrawable.getHeight();
    
      if (width != alphaDrawable.getWidth() || height != alphaDrawable.getHeight()) {
        throw new IllegalStateException("image size mismatch!");
      }
    
      Bitmap destBitmap = Bitmap.createBitmap(width, height,
          Bitmap.Config.ARGB_8888);
    
      int[] pixels = new int[width];
      int[] alpha = new int[width];
    
      for (int y = 0; y < height; y++)
      {
          rgbDrawable.getPixels(pixels, 0, width, 0, y, width, 1);
          alphaDrawable.getPixels(alpha, 0, width, 0, y, width, 1);
    
          for (int x = 0; x < width; x++) 
          {
              // Replace the alpha channel with the r value from the bitmap.
              pixels[x] = (pixels[x] & 0x00FFFFFF) | ((alpha[x] << 8) & 0xFF000000);
          }
    
        destBitmap.setPixels(pixels, 0, width, 0, y, width, 1);
      }
    
      alphaDrawable.recycle();
      rgbDrawable.recycle();
    
      return destBitmap;
    
  • Second Method

        float[] nlf =  new float[] {
                                0, 0, 0, 0, 0,
                                0, 0, 0, 0, 0,
                                0, 0, 0, 0, 0,
                                1, 0, 0, 0, 0};
    
    ColorMatrix sRedToAlphaMatrix = new ColorMatrix(nlf);
    
    ColorMatrixColorFilter sRedToAlphaFilter = new ColorMatrixColorFilter(sRedToAlphaMatrix);
    
    // Load RGB data
    Bitmap rgb = rgbDrawable;
    
    // Prepare result Bitmap
    Bitmap target = Bitmap.createBitmap(rgb.getWidth(), rgb.getHeight(), Bitmap.Config.ARGB_8888);
    
    Canvas c = new Canvas(target);
    c.setDensity(Bitmap.DENSITY_NONE);
    
    // Draw RGB data on our result bitmap
    c.drawBitmap(rgb, 0, 0, null);
    
    // At this point, we don't need rgb data any more: discard!
    rgb.recycle();
    rgb = null;
    
    // Load Alpha data
    Bitmap alpha = alphaDrawable;
    
    // Draw alpha data on our result bitmap
    final Paint grayToAlpha = new Paint();
    grayToAlpha.setColorFilter(sRedToAlphaFilter);
    grayToAlpha.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    c.drawBitmap(alpha, 0, 0, grayToAlpha); 
    
    // Don't need alpha data any more: discard!
    alpha.recycle();
    alpha = null;
    
    return target;
    
Souandios
  • 534
  • 1
  • 4
  • 12
  • see this http://stackoverflow.com/questions/16140501/android-apply-colormatrix-colorfilter-on-part-of-imageview-with-a-mask/16161595#16161595 it should give you an idea what to do – pskink Jun 19 '13 at 08:15
  • @pskink Thanks for your reply. I have already tried using color filter, but that is not efficient. Please see my edited question. – Souandios Jun 19 '13 at 08:22
  • For anyone googling, here's the total everyday solution for masking: http://stackoverflow.com/a/23991540/294884 Souandios, you're looking for something **faster** than that?? – Fattie Jun 02 '14 at 12:27

0 Answers0