Search code examples
c#androidxamarin.androidalphaporter-duff

PorterDuff.Mode.Multiply not working as intended? Black background instead of transparent


I have a problem with PorterDuff.Mode.Multiply, it seems that all alpha channels are set as 'black'. Is this as intended? In photoshop / gimp etc the effect leaves transparency where it should be. Darken leaves the transparency alone, but still applies the effect to values with RGB, this is what I want, but with Multiply's effect.

It's just the PorterDuff.Mode.Multiply that causes the black background problem with the overlay.

Bitmap photo = ((BitmapDrawable)ivPhoto.Drawable).Bitmap;
Bitmap overlay = ((BitmapDrawable)overlay.Drawable).Bitmap;

Point ss = getScaledSize(photo.Width, photo.Height, scrSize.X, scrSize.Y);

Bitmap bresult = Bitmap.CreateScaledBitmap(photo, ss.X, ss.Y, true);

Canvas myCanvas = new Canvas(bresult);

Paint myPaintStyle = new Paint(PaintFlags.FilterBitmap);

myPaintStyle.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.Multiply));

myCanvas.DrawBitmap(
    overlay,  //img
    ivHair.GetX(), ivHair.GetY(),  //x,y
    myPaintStyle); //style


return bresult;

Help? :)

This is in Mono for Android

Edit: DstIn has the same undesirable effect. (alpha is black)


Solution

  • I had the same problem for an entire day. I was getting crazy .

    What worked for me is this line of code:

    yourView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
    

    Place it in the class constructor and now the black background should disappear.