透明图像背景问题android

时间:2018-07-04 04:47:11

标签: android android-imageview android-gallery

透明图像背景在放入Android Gallery后会转换为黑色。如果我从图库上传透明图像,则图像背景显示为黑色

1 个答案:

答案 0 :(得分:0)

    bitmap = eraseBG(bitmap, -1);         // use for white background
    bitmap = eraseBG(bitmap, -16777216);  // use for black background


private static Bitmap eraseBG(Bitmap src, int color) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap b = src.copy(Config.ARGB_8888, true);
    b.setHasAlpha(true);

    int[] pixels = new int[width * height];
    src.getPixels(pixels, 0, width, 0, 0, width, height);

    for (int i = 0; i < width * height; i++) {
        if (pixels[i] == color) {
            pixels[i] = 0;
        }
    }

    b.setPixels(pixels, 0, width, 0, 0, width, height);

    return b;
}