fresco icon indicating copy to clipboard operation
fresco copied to clipboard

Creating a resized bitmap using PlatformBitmapFactory doesn't have alpha value

Open raylee4204 opened this issue 11 years ago • 5 comments

I'm trying to mask a bitmap using a 9patch image to shape it into a custom shape.

This is what I've tried but the result bitmap returns black pixels where the area is masked.

Doesn't PlatformBitmapFactor create a bitmap with alpha value?

 @Override
    public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) {
        CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(mWidth, mHeight);
        try {
            Bitmap result = bitmapRef.get();
            Canvas canvas = new Canvas(result);
            Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            paint.setFilterBitmap(false);

            canvas.drawBitmap(sourceBitmap, 0, 0, paint);

            mask.setBounds(0, 0, mWidth, mHeight);
            mask.getPaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
            mask.draw(canvas);
            return CloseableReference.cloneOrNull(bitmapRef);
        } finally {
            CloseableReference.closeSafely(bitmapRef);
        }
    }

raylee4204 avatar Apr 24 '15 20:04 raylee4204

Bitmap should be ARGB_8888. What color is set to mask.getPaint()? On some Samsung devices default paint color is FF000000, i.e. black, not transparent. Can you do mask.getPaint().setColor(0) before calling mask.draw? Also, I don't see what is mask doing exactly, so can't provide more help without having more details.

plamenko avatar Apr 27 '15 16:04 plamenko

Sorry for the late response. mask is a NinePatchDrawable created from resource file, which I don't set any color to - meaning I'm using as is once it's created.

raylee4204 avatar May 04 '15 14:05 raylee4204

Can you try this:

result.setHasAlpha(true);

Let us know if that works.

tyronen avatar May 11 '15 11:05 tyronen

This works. Thanks!

raylee4204 avatar May 11 '15 19:05 raylee4204

How do you manipulate the position of the bitmap to be drawn - similar to center crop

raylee4204 avatar May 11 '15 20:05 raylee4204