DrawBitmap on a canvas with a transparent bitmap, replace the original pixels

My application allows the user to edit the image. The image was edited in β€œslices”: the user selects part of a large image for editing (1), the user edits it (2, 3), and then, when the user finishes, the edited fragment is copied back to the original image (4). You can see the simplified procedure in the following figure.

Sample workflow

  • To edit a slice, I create a bitmap image of the cropped area that the user edits (2,3).

  • When the user finishes, I just drawBitmap() slice into the original image (4). The process is more complicated, because the original image has a transformation matrix, which I have to invert, etc., but for simplicity this is enough.

The problem occurs when the user clears some pixels in the slice (3) . I cannot find the correct PorterDuff / Paint mode, so the edited fragment replaces part of the original image even with transparent pixels. I want to get the result depicted in (4)

It is best to use PorterDuff.SRC , but as you see in the image below, the transparent pixels become black in the original image. If I set the color to Transparent, the whole result will be black.

  mBlitPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)); mImageCanvas.drawBitmap(mArenaBitmap, invertedMatrix, mBlitPaint); 

I also tried SRC_OVER and even mImageCanvas.drawARGB (0xff,0,0,0), , but no luck. In the first case, transparent pixels are simply ignored. In the second, transparent pixels are painted black.

Sample result

+5
source share
1 answer

I also had this problem, and I could not find a solution, fortunately, my goal was to show something that would not change it and save ... I used different layers and it seemed to work. but if you find a solution to this, I would like to know how you did it.

0
source

Source: https://habr.com/ru/post/1207467/


All Articles