在位图上绘制位图并将一些像素设置为透明

时间:2014-09-18 19:50:14

标签: android bitmap transparency

我有自定义View和两个Bitmap。我像这样把它画在另一个上面

canvas.drawBitmap(backImage,0,0,null);
canvas.drawBitmap(frontImage,0,0,null);

在绘画之前,我使用setPixel(...)

Bitmap函数在frontImage中设置一些透明像素
frontImage.setPixel(x,y, Color.TRANSPARENT);

不是在x,y处查看backImage的像素,而是看到黑色......

2 个答案:

答案 0 :(得分:0)

对此可能有一个非常简单的解决方案。您的图像源材料是什么?如果您要从文件加载它们,您可能需要做的就是将源文件转换为PNG' s。 PNG维护透明度信息,大多数渲染引擎会在屏幕上将它们分层时将其考虑在内。

答案 1 :(得分:-1)

另一种可能性。我在PC上的Java游戏中使用了这种技术。它使用了一个小透明度的课程,我在这个地方偶然发现了几年:

 /*************************************************************************
 * The Transparency class was also developed by a thrid party. Info
 * on its use can be found at:
 *
 * http://www.rgagnon.com/javadetails/java-0265.html
 *
 *************************************************************************/
//Transparency is a "Static", "Inner" class that will set a given color
//as transparent in a given image.
class Transparency {
    public static Image set(Image im, final Color color) {
        ImageFilter filter = new RGBImageFilter() { //Inner - Inner class -- very bad
            // the color we are looking for... Alpha bits are set to opaque
            public int markerRGB = color.getRGB() | 0xFF000000;

            public final int filterRGB(int x, int y, int rgb) {
                if ( ( rgb | 0xFF000000 ) == markerRGB ) {
                    // Mark the alpha bits as zero - transparent
                    return 0x00FFFFFF & rgb;
                }
                else {
                    // nothing to do
                    return rgb;
                }
            }
        };
        //apply the filter created above to the image
        ImageProducer ip = new FilteredImageSource(im.getSource(), filter);
        return Toolkit.getDefaultToolkit().createImage(ip);
    }
}

将Java Image对象作为输入,它将采用您给出的任何颜色,并对图像执行数学运算,使颜色变得透明。

祝你好运。