Question

I am trying to scale down a Bitmap that contains transparency. Unfortunately, all three methods I've tried result in white where there should be full transparency. I would like to maintain the transparency.

The bitmap I am scaling is always square, so my functions assume that:

private static Bitmap scale1(int edge, final Bitmap res) {
    int factor = 
        edge == 0 ? Fixed32.ONE : Fixed32.div(res.getHeight(), edge);
    Bitmap scaled = PNGEncodedImage.encode(res).
                        scaleImage32(factor, factor).getBitmap();
    return scaled;
}

private static Bitmap scale2(int edge, final Bitmap res) {
    Bitmap val = new Bitmap(edge,edge);
    val.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
    res.scaleInto(val, Bitmap.FILTER_BILINEAR);
    return val;
}

private static Bitmap scale3(int edge, final Bitmap res) {
    Bitmap val = new Bitmap(edge,edge);
    val.createAlpha(Bitmap.ALPHA_BITDEPTH_8BPP);
    Graphics g = new Graphics(val);
    int[] pathX = new int[] {0, 0+edge, 0+edge, 0};
    int[] pathY = new int[] {0, 0, 0+edge, 0+edge};
    byte[] pathPointTypes = new byte[] {
        Graphics.CURVEDPATH_END_POINT,Graphics.CURVEDPATH_END_POINT,
        Graphics.CURVEDPATH_END_POINT,Graphics.CURVEDPATH_END_POINT};
    int factor = 
        edge == 0 ? Fixed32.ONE : Fixed32.div(res.getHeight(), edge);
    g.drawTexturedPath(pathX, pathY, pathPointTypes, null,
            0, 0, factor, Fixed32.toFP(0), Fixed32.toFP(0), factor, res);
    return val;
}
Was it helpful?

Solution

It looks like Graphics.drawBitmap(...) uses Graphics.drawRGB(...) instead of Graphics.drawARGB(...)

The distinction is whether or not the alpha channel is used to draw the bitmap. I've tried using Graphics.drawARGB(...) directly for some other transparent bitmap work I've been doing, and that made the difference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top