我正在制作一个图像编辑应用程序,用户可以在其中触摸屏幕并在他们的照片上放置一个“贴纸”。
我的贴纸都有绿色背景(0、255、0),我想让它们透明。但是,像素不是透明的,而是黑色的。
这是我加载透明位图的代码:
private Bitmap transparentBitmap(int id)
{
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id, bmOptions);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int index = y * width + x;
if (pixels[index] == Color.GREEN)
{
pixels[index] = Color.argb(0, 0, 0, 0);
}
}
}
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}
我在应用程序的开头加载所有贴纸,当用户触摸屏幕某处时,图形从我的 Assets 类的静态位图中复制:
private Bitmap getGFX(stickerTypes type)
{
switch(type)
{
case sweatDrop: return Assets.sweatDrop_GFX.copy(Config.ARGB_4444, true);
case veins: return Assets.veins_GFX.copy(Config.ARGB_4444, true);
default: return null;
}
}
所以就像我说的,我的实际贴纸不是以透明背景显示,而是以黑色背景显示。它们被保存为 .png。
我能想到的唯一解释是贴纸的背景像素被设置为相对于活动的背景而不是用户图片的背景透明,但这没有多大意义,因为活动是橙色的,而不是黑色的。