Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android drawBitmap with PorterDuff.Mode.DST_IN ,the area which should be transparent turn out to be black

I'm trying to draw a image with a mask of specific shape. I'm using Facebook's library Fresco and here is the code:

    ImageRequest request = builder.setPostprocessor(new BasePostprocessor() {
        @Override
        public String getName() {
            return "ChatViewImageProcessor";
        }

        @Override
        public void process(Bitmap bitmap) {
            int bitmap_width = bitmap.getWidth();
            int bitmap_height = bitmap.getHeight();
            ViewGroup.LayoutParams params = iv.getLayoutParams();
            if (width == -1 && height == -1) {//not relavant with question
                horizontal[0] = bitmap_width >= bitmap_height;
                params.width = horizontal[0] ? max_image_size : (int) (max_image_size * 1.0 * bitmap_width / bitmap_height);
                params.height = (horizontal[0] ? (int) (max_image_size * 1.0 * bitmap_height / bitmap_width) : max_image_size);

            }

            Bitmap temp = Bitmap.createBitmap(params.width, params.height, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(temp);
            if (bg != null) {
                bg.setBounds(0, 0, params.width, params.height);
                bg.draw(canvas);
            }
            Logger.out("temp[0,0]:" + Util.getColorNote(temp.getPixel(0, 0), true));
            canvas = new Canvas(bitmap);

            Rect src = new Rect(0, 0, temp.getWidth(), temp.getHeight());
            Rect dst = new Rect(0, 0, bitmap_width, bitmap_height);
            canvas.drawBitmap(temp, src, dst, paint);//<----------problem here

            temp.recycle();
            Logger.out("result[0,0]:" + Util.getColorNote(bitmap.getPixel(0, 0), true));
        }
    }).build();

    PipelineDraweeController controller = (PipelineDraweeController)
            Fresco.newDraweeControllerBuilder()
                    .setImageRequest(request)
                    .setOldController(iv.getController())
                    .build();
    iv.setController(controller);

This code works just fine on 5.0.2... I can't post image here,see https://i.sstatic.net/9DBbj.jpg

but fails on 4.4.4 and 4.1.2. I can't post image here,see https://i.sstatic.net/8wbLp.jpg

I also printed the top-left pixel's color of "temp" and clipped "bitmap",on 5.0.2,that is #00000000 and #00000000, on 4.4.4 and 4.1.2 they are #00000000 and #FF000000

like image 621
Michael Archangel Avatar asked Dec 02 '25 13:12

Michael Archangel


1 Answers

I'm so happy to find the answer on Fresco's github issue page here

As it says, I just need to call

    bitmap.setHasAlpha(true);

in

    public void process(Bitmap bitmap)
like image 197
Michael Archangel Avatar answered Dec 05 '25 03:12

Michael Archangel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!