Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AvoidXfermode is deprecated since API 16, is there a replacement?

I need to draw a bitmap on another bitmap, but I only want to draw on top of pixels that have a specific color (transparent in this case) . I understand that AvoidXfermode could do that, but it is deprecated since API 16. Is there a different method to this now ?

Thank you

like image 287
EyalBellisha Avatar asked Apr 21 '13 14:04

EyalBellisha


2 Answers

I received the right answer in my private inbox, so I will share it here: There is no replacement method. AvoidXfermode was deprecated because it is not supported in hardware. However, it can still be used when drawing on bitmaps.

Another method, of course, is to go pixel by pixel, and replace the ones with a specific color, but that's not what I was looking for.

like image 182
EyalBellisha Avatar answered Sep 27 '22 16:09

EyalBellisha


Here we go, assuming bitmap sizes are the same.

var image1, image2;

var newCanvas = document.createElement('canvas');
var newContext = newCanvas.getContext('2d');
newCanvas.width = image1.width;
newCanvas.height = image1.height;
newContext.drawImage(image1, 0, 0);
var imgData = newContext.getImageData(0,0,newCanvas.width, newCanvas.height);
newContext.drawImage(image2, 0, 0);
var imgData2 = newContext.getImageData(0,0,newCanvas.width, newCanvas.height);

for (var i = 0; i < imgData.data.length; i += 4) {
    if( imgData.data[i] < 20        //If Red is less than 20
        && imgData.data[i+1] == 40    //If Green is 40
        && imgData.data[i+2] >= 240   //If Blue is over 240
        && imgData.data[i+3] >= 240) //If Alpha is over 240
    {
        imgData.data[i] = imgData2.data[i];
        imgData.data[i+1] = imgData2.data[i+1];
        imgData.data[i+2] = imgData2.data[i+2];
        imgData.data[i+3] = imgData2.data[i+3];
    }
}
imgData2 = null;
newContext.putImageData(imgData, 0, 0);

That should just copy it over as is if any pixel meets those parameters.

The end result is either imgData, a byte array of data that you can draw using putImageData, or a canvas (which you can essentially use as a new Image)

like image 21
Jack Franzen Avatar answered Sep 27 '22 17:09

Jack Franzen