网上的一般的方法为~
先获取其bitmapData对象..
当进行点击操作时使用..
bitmapData.getPixel32(x,y)获取..
点击位置的是否透明..
然后再操作...
而mosueOver与mouseOut等事件稍为复杂点..
需要侦听mouseMove..
然后对经过的坐标进行透明判断~~再抛出相应的事件...
今天介绍的方法原理跟上面差不多..
不过我们使用Sprite的另一个属性hitArea..(关于hitArea的说明请查看这里)
在图像加载完毕后..
先绘制一个去除了透明部份的Sprite..
然后把该Sprite指定为源对象的hitArea.
代码也很简单:
main.mxml
- <?xmlversion="1.0"encoding="utf-8"?>
- <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"xmlns:local="*">
- <mx:Script>
- <![CDATA[
- importmx.managers.CursorManager;
- privatefunctionabc(e:MouseEvent):void
- {
- if(e.type=="mouseOver")
- {
- CursorManager.setBusyCursor();
- }else
- {
- CursorManager.removeBusyCursor();
- }
- }
- ]]>
- </mx:Script>
- <local:MyImagesource="10020601.png"mouseOver="abc(event)"mouseOut="abc(event)"y="25"/>
- <mx:Imagesource="10020601.png"mouseOver="abc(event)"mouseOut="abc(event)"x="344"y="25"/>
- </mx:Application>
- package
- {
- importflash.display.BitmapData;
- importflash.display.DisplayObject;
- importflash.display.Sprite;
- importflash.events.Event;
- importflash.geom.Matrix;
- importflash.utils.setTimeout;
- importmx.controls.Image;
- /**
- *自定义Image类,使之忽略PNG透明区域
- *@authorL4cd.Net
- */
- publicclassMyImageextendsImage
- {
- privatevarht:Sprite=newSprite();
- publicfunctionMyImage(){
- addChild(ht);
- hitArea=ht;
- //指定hitArea为ht对象
- ht.visible=false;
- ht.mouseEnabled=false;
- mouseChildren=false;
- addEventListener(Event.COMPLETE,complete,false,99,true);
- setTimeout(update,50)
- }
- privatefunctioncomplete(e:Event):void{
- setTimeout(update,50)
- }
- privatefunctionupdate():void{
- if(!content)return;
- varloader:DisplayObject=content.parentasDisplayObject;
- varbit:BitmapData=newBitmapData(loader.width,loader.height,true,0x00000000);
- varmat:Matrix=newMatrix();
- mat.scale(loader.scaleX,loader.scaleY);
- bit.draw(loader,mat);
- //重绘图象到bit
- ht.graphics.clear();
- ht.graphics.beginFill(0);
- for(varx:uint=0;x<bit.width;x++){
- for(vary:uint=0;y<bit.height;y++){
- if(bit.getPixel32(x,y))ht.graphics.drawRect(x,y,1,1);
- }
- }
- //以graphics画出bit的无透明区域
- ht.graphics.endFill();
- }
- }
- }