2014-09-18 69 views
0

我有一个自定义View和两个Bitmap s。我得出这样的通过位图绘制位图并设置一些像素透明

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

高于其他的一幅画我设置一些像素的frontImage透明使用Bitmap

frontImage.setPixel(x,y, Color.TRANSPARENT); 

setPixel(...)函数,而不是在x观看backImage的像素之前,Y我见黑色...

+0

你对'Paint'有什么新的XferMode? – 2014-09-18 20:15:08

+0

我尝试没有'涂料'上面和XferMode'PorterDuff.Mode.DST_ATOP',但我得到了相同的结果...黑色 – gdros 2014-09-18 20:20:54

+0

你试过'PorterDuff.Mode.CLEAR'吗? – 2014-09-18 20:26:54

回答

0

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

+0

我使用PNG文件... – gdros 2014-09-18 20:43:06

+0

明显的后续问题, :你使用的编辑器保存透明度信息(photoshop或paint.net)吗?更基本的编辑器会将透明信息保存为默认背景颜色。 – codingCat 2014-09-19 14:49:25

-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对象作为输入,它会采取什么都颜色,你给它,并在图像变换的执行数学颜色透明。

祝你好运。

+0

这个问题清楚地标记为“android”,Android没有这些类。 – 2015-05-26 19:39:49

+0

我并不是建议他们这样做。这个例子是作为一个起点提出的,在那里请求者可以编写他们自己的android特定版本。但是,谢谢你停下来指出你以为我错了。 – codingCat 2015-06-01 14:26:05