ユーザーが画面に触れて写真の上に「ステッカー」を配置できる画像編集アプリを作成しています。
私のステッカーはすべて、透明にしたい緑色の背景 (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 として保存されます。
私が考えることができる唯一の説明は、ユーザーの写真の背景ではなく、アクティビティの背景に対してステッカーの背景ピクセルが透明に設定されているということですが、それはあまり意味がありません。アクティビティは黒ではなくオレンジです。