Rubbish drawn instead of transparent color when using BitmapRegionDecoder

123 Views Asked by At

So, simple task: I have a big picture, which has transparent parts. I want to decode rectangle part of it as a bitmap using BitmapRegionDecoder method decodeRegion. While non-transparent parts of picture are decoded well, I get some crap instead of transparent parts. Did anyone face something familiar? The code I use:

// colNumber, rowNumber are some proper integers from 0 to 4
InputStream is = playingTable.getResources().openRawResource(R.drawable.picture);
        try {
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(is, false);
            int cellWidth = decoder.getWidth() / 5;
            int cellHeight = decoder.getHeight() / 5;
            Rect rect = new Rect(colNumber * cellWidth, rowNumber * cellHeight, (colNumber + 1) * cellWidth, (rowNumber + 1) * cellHeight);
            bmp = decoder.decodeRegion(rect, null);
            is.close();
        } catch (IOException e) {
            Log.i("Exception: ", e.toString());
        }

// in draw method:
        canvas.drawBitmap(bmp, x, y, null);
1

There are 1 best solutions below

0
On BEST ANSWER

Use a Paint object for drawing.It is capable of alpha channels.You may need to run through the image pixel by pixel in a nested loop.

Android Developers - Paint