【问题标题】:Overlaying a transparent bitmap over an imageview在图像视图上覆盖透明位图
【发布时间】:2013-07-21 16:16:10
【问题描述】:

我正在制作一个图像编辑应用程序,用户可以在其中触摸屏幕并在他们的照片上放置一个“贴纸”。

我的贴纸都有绿色背景(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。

我能想到的唯一解释是贴纸的背景像素被设置为相对于活动背景而不是用户图片背景的透明,但这没有多大意义,因为活动的背景是橙色,而不是黑色。

【问题讨论】:

    标签: android bitmap transparency alpha image-editing


    【解决方案1】:

    当前您正在将 Colors.argb(...) 中的 alpha 分量设置为零

    pixels[index] = Color.argb(0, 0, 0, 0);
    

    相反,将其设置为最大 alpha:

    pixels[index] = Color.argb(255, 0, 0, 0);
    

    【讨论】:

      【解决方案2】:

      只需使用油漆制作像素并更改属性“设置 Alpha”以使其透明

      希望它会起作用!

      【讨论】:

        猜你喜欢
        • 2015-08-12
        • 1970-01-01
        • 1970-01-01
        • 2020-05-30
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 2017-05-21
        • 1970-01-01
        相关资源
        最近更新 更多