2012-11-08 93 views
2

我需要绘制一个位图,但不绘制它的透明像素?如何绘制位图,但不绘制透明像素?

实施例图象这里:http://i.stack.imgur.com/QvJTZ.png

我写了这个代码:

import flash.display.Bitmap; 
import flash.events.Event; 
import flash.geom.Matrix; 

var s:S = new S(); 
var m_bitmapData = new BitmapData(s.width, s.height, true, 0x00000000); 
m_bitmapData.draw(s); 
var hole=new Sprite(); 

var hole_matrix:Matrix 
hole.graphics.beginFill(0x000000); 
hole.graphics.drawCircle(0,0,30); 


var bmp:Bitmap = new Bitmap(m_bitmapData); 
bmp.x = 50 
bmp.y =50 

stage.addChild(bmp); 

addEventListener(Event.ENTER_FRAME,asd); 
function asd(e:Event):void{ 
    hole_matrix=new Matrix(); 
    hole_matrix.translate(mouseX-bmp.x,mouseY-bmp.y); 
    m_bitmapData.draw(hole,hole_matrix); 
} 

但结果是诸如图片 “NO”。

有人能解释一下吗?

回答

0

保留alpha通道,然后将其复制回来。

var tempBD:BitmapData=new BitmapData(m_bitmapData.width,m_bitmapData.height,true,0); 
... 
function asd(e:Event):void { 
    ... 
    tempBD.copyChannel(m_bitmapData,m_bitmapData.rect,new Point(),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA); 
    m_bitmapData.draw(hole,hole_matrix); 
    m_bitmapData.copyChannel(tempBD,tempBD.rect,new Point(),BitmapDataChannel.ALPHA,BitmapDataChannel.ALPHA); 
} 

基本上你要做的:你有一个BitmapData其他地方的大小与画布的BitmapData一样的,那么您复制Alpha通道进入该BitmapData,画出你想要什么,然后复制Alpha通道回来。无论透明度如何,仍然透明。请注意,如果您不希望Alpha通道发生更改,您只能将其删除一次,然后再恢复。

+0

非常感谢! –