开发者

Scale a Bitmap and preserve alpha, on BlackBerry

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.drawTexturedPat开发者_JAVA百科h(pathX, pathY, pathPointTypes, null,
            0, 0, factor, Fixed32.toFP(0), Fixed32.toFP(0), factor, res);
    return val;
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜