忽略图片透明区域的事件(Flex)

网上的一般的方法为~
先获取其bitmapData对象..

当进行点击操作时使用..
bitmapData.getPixel32(x,y)获取..
点击位置的是否透明..
然后再操作...

而mosueOver与mouseOut等事件稍为复杂点..
需要侦听mouseMove..
然后对经过的坐标进行透明判断~~再抛出相应的事件...

今天介绍的方法原理跟上面差不多..
不过我们使用Sprite的另一个属性hitArea..(关于hitArea的说明请查看这里)
在图像加载完毕后..
先绘制一个去除了透明部份的Sprite..
然后把该Sprite指定为源对象的hitArea.

代码也很简单:
main.mxml

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"xmlns:local="*">
  3. <mx:Script>
  4. <![CDATA[
  5. importmx.managers.CursorManager;
  6. privatefunctionabc(e:MouseEvent):void
  7. {
  8. if(e.type=="mouseOver")
  9. {
  10. CursorManager.setBusyCursor();
  11. }else
  12. {
  13. CursorManager.removeBusyCursor();
  14. }
  15. }
  16. ]]>
  17. </mx:Script>
  18. <local:MyImagesource="10020601.png"mouseOver="abc(event)"mouseOut="abc(event)"y="25"/>
  19. <mx:Imagesource="10020601.png"mouseOver="abc(event)"mouseOut="abc(event)"x="344"y="25"/>
  20. </mx:Application>
MyImage.as,自定义Image类..
Java代码
  1. package
  2. {
  3. importflash.display.BitmapData;
  4. importflash.display.DisplayObject;
  5. importflash.display.Sprite;
  6. importflash.events.Event;
  7. importflash.geom.Matrix;
  8. importflash.utils.setTimeout;
  9. importmx.controls.Image;
  10. /**
  11. *自定义Image类,使之忽略PNG透明区域
  12. *@authorL4cd.Net
  13. */
  14. publicclassMyImageextendsImage
  15. {
  16. privatevarht:Sprite=newSprite();
  17. publicfunctionMyImage(){
  18. addChild(ht);
  19. hitArea=ht;
  20. //指定hitArea为ht对象
  21. ht.visible=false;
  22. ht.mouseEnabled=false;
  23. mouseChildren=false;
  24. addEventListener(Event.COMPLETE,complete,false,99,true);
  25. setTimeout(update,50)
  26. }
  27. privatefunctioncomplete(e:Event):void{
  28. setTimeout(update,50)
  29. }
  30. privatefunctionupdate():void{
  31. if(!content)return;
  32. varloader:DisplayObject=content.parentasDisplayObject;
  33. varbit:BitmapData=newBitmapData(loader.width,loader.height,true,0x00000000);
  34. varmat:Matrix=newMatrix();
  35. mat.scale(loader.scaleX,loader.scaleY);
  36. bit.draw(loader,mat);
  37. //重绘图象到bit
  38. ht.graphics.clear();
  39. ht.graphics.beginFill(0);
  40. for(varx:uint=0;x<bit.width;x++){
  41. for(vary:uint=0;y<bit.height;y++){
  42. if(bit.getPixel32(x,y))ht.graphics.drawRect(x,y,1,1);
  43. }
  44. }
  45. //以graphics画出bit的无透明区域
  46. ht.graphics.endFill();
  47. }
  48. }
  49. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值