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

Android 在位图上绘制位图,并将一些像素设置为透明,android,bitmap,transparency,Android,Bitmap,Transparency,我有一个自定义视图和两个位图。我像这样把一个画在另一个上面 canvas.drawBitmap(backImage,0,0,null); canvas.drawBitmap(frontImage,0,0,null); 在绘制之前,我使用setPixel在frontImage中设置了一些透明像素。。。位图的功能 我看到的不是x、y位置的背景图像像素,而是黑色…可能有一个非常简单的解决方案。你的图片来源是什么?如果您是从文件加载它们,那么您可能需要做的就是将源文件转换为PNG。PNG保持透明度信息

我有一个自定义视图和两个位图。我像这样把一个画在另一个上面

canvas.drawBitmap(backImage,0,0,null);
canvas.drawBitmap(frontImage,0,0,null);
在绘制之前,我使用setPixel在frontImage中设置了一些透明像素。。。位图的功能


我看到的不是x、y位置的背景图像像素,而是黑色…

可能有一个非常简单的解决方案。你的图片来源是什么?如果您是从文件加载它们,那么您可能需要做的就是将源文件转换为PNG。PNG保持透明度信息,大多数渲染引擎在屏幕上将它们分层时都会考虑到这一点。

另一种可能性。我在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图像对象作为输入,它将采用您给它的任何颜色,并对图像执行数学运算,使颜色透明


祝你好运。

你用什么来制作新的XferMode画作?我尝试过不使用上面的画作,并在顶部使用XferMode PorterDuff.Mode.DST_,但得到了相同的结果。。。black您是否尝试过PorterDuff.Mode.CLEAR?这会在x,y处产生所需的结果,但每隔一个像素都会变黑…使用不同的画法和XFerMode绘制位图的其余部分。回答:您是否使用了保留透明度信息的编辑器photoshop或Paint.net?更基本的编辑器会将透明信息保存为默认背景色。问题清楚地标记为android,android没有这些类。我并不是说他们有。这个例子是作为一个起点提供的,提问者可以设计自己的android特定版本。但是谢谢你过来指出你认为我错了。
 /*************************************************************************
 * 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);
    }
}