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

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

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

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

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

代码也很简单: 
main.mxml 

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*">     
  3.     <mx:Script>          
  4.     <![CDATA[              
  5.         import mx.managers.CursorManager;              
  6.         private function abc(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:MyImage source="10020601.png" mouseOver="abc(event)" mouseOut="abc(event)"  y="25"/>   
  19. <mx:Image source="10020601.png" mouseOver="abc(event)" mouseOut="abc(event)"  x="344" y="25"/>   
  20. </mx:Application>  
MyImage.as,自定义Image类.. 

Java代码  
  1. package  
  2. {   
  3.     import flash.display.BitmapData;   
  4.     import flash.display.DisplayObject;   
  5.     import flash.display.Sprite;   
  6.     import flash.events.Event;   
  7.     import flash.geom.Matrix;   
  8.     import flash.utils.setTimeout;   
  9.     import mx.controls.Image;   
  10.     /**  
  11.     * 自定义Image类,使之忽略PNG透明区域   
  12.     * @author L4cd.Net  
  13.     */    
  14.     public class MyImage extends Image   
  15.     {   
  16.         private var ht:Sprite = new Sprite();   
  17.         public function MyImage(){   
  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.         private function complete(e:Event):void{   
  28.             setTimeout(update,50)   
  29.         }   
  30.         private function update():void{   
  31.             if(!content)return;   
  32.             var loader:DisplayObject = content.parent as DisplayObject;   
  33.             var bit:BitmapData = new BitmapData(loader.width,loader.height,true,0x00000000);   
  34.             var mat:Matrix = new Matrix();   
  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(var x:uint=0;x<bit.width;x++){   
  41.                 for(var y: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. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值