【发布时间】:2018-07-04 04:47:10
【问题描述】:
透明图像背景在放入 Android 图库后转换为黑色。如果我从图库上传透明图像,则图像背景显示黑色
【问题讨论】:
标签: android android-imageview android-gallery
透明图像背景在放入 Android 图库后转换为黑色。如果我从图库上传透明图像,则图像背景显示黑色
【问题讨论】:
标签: android android-imageview android-gallery
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;
}
【讨论】: