使用Flex坐标系统及转换

 

 

 flash和flex针对不同的目的,提供了3种不同的坐标系。

          全局的就是(stage级别的)

          本地坐标系(组件级别的)

          内容坐标系(相对于本地坐标系说的)

    这些坐标系的点是可以转换的,并且有相应的方法,看来adobe想得挺周到。我们一个一个的说一下:

    全局

           这个坐标系的原点在整个flash舞台的左上角,MouseEvent实例的stageX,stageY就是这个坐标系中的值。

    本地

        坐标原点是相对的组件的左上角,MouseEvent中的localX,localY就是相对这个坐标系说的。

    内容

        这个东西比较抽象了UIComponent类实例的contentMouseX  和 contentMouseY 就是了,这个主要针对有滚动条的组件说的,有滚动条了,内容肯定不少,内容所占的区域的坐标就是这个坐标系了。

下面有个官方的图说明了三个坐标系的关系及位置:

 


 

A. Content bounds B. Component bounds C. Stage bounds

 

 

坐标转换还有现成的方法:

contentMouseX

返回mouse的内容坐标x值

contentMouseY

返回mouse的内容坐标Y值

contentToGlobal   

将内容坐标转换成全局坐标

contentToLocal 

将内容坐标转换成内容坐标

globalToContent   

将全局的转成内容坐标

globalToLocal 

全局的转成本地的

localToContent

本地到内容坐标

localToGlobal 

本地到全局坐标

下面是一个小例子:

  1. <?xml version= "1.0" ?>  
  2. <!-- containers\intro\MousePosition.mxml -->  
  3. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"   
  4.     backgroundColor="white" >  
  5.      
  6.     <mx:Script>  
  7.         <!--[CDATA[  
  8.           import mx.controls.Alert;  
  9.           // Handle the mouseDown event generated   
  10.           // by clicking in the application.   
  11.           private  function handleMouseDown( event :MouseEvent): void  {  
  12.                  
  13.             // Convert the mouse position to global coordinates.   
  14.             // The localX and localY properties of the mouse event contain   
  15.             // the coordinates at which the event occurred relative to the   
  16.             // event target, typically one of the   
  17.             // colored internal Canvas controls.   
  18.             // A production version of this example could use the stageX   
  19.             // and stageY properties, which use the global coordinates,   
  20.             // and avoid this step.   
  21.             // This example uses the localX and localY properties only to   
  22.             // illustrate conversion between different frames of reference.   
  23.             var pt:Point = new  Point( event .localX,  event .localY);  
  24.             pt = event .target.localToGlobal(pt);  
  25.                  
  26.             // Convert the global coordinates to the content coordinates   
  27.             // inside the outer c1 Canvas control.   
  28.             pt = c1.globalToContent(pt);  
  29.                 
  30.             // Figure out which quadrant was clicked.   
  31.             var whichColor:String = "border area" ;  
  32.                
  33.             if  (pt.x < 150) {  
  34.                 if  (pt.y < 150)  
  35.                     whichColor = "red" ;  
  36.                 else   
  37.                     whichColor = "blue" ;  
  38.             }  
  39.             else  {  
  40.                 if  (pt.y < 150)  
  41.                     whichColor = "green" ;  
  42.                 else   
  43.                     whichColor = "magenta" ;  
  44.             }  
  45.                  
  46.             Alert.show("You clicked on the "  + whichColor);  
  47.           }  
  48.         ]]-->  
  49.     </mx:Script>  
  50.     <!-- Canvas container with four child Canvas containers -->  
  51.     <mx:Canvas id="c1"   
  52.         borderStyle="none"   
  53.         width="300"  height= "300"   
  54.         mouseDown="handleMouseDown(event);" >  
  55.          
  56.         <mx:Canvas  
  57.             width="150"  height= "150"   
  58.             x="0"  y= "0"   
  59.             backgroundColor="red" >  
  60.             <mx:Button label="I'm in Red" />  
  61.         </mx:Canvas>  
  62.         <mx:Canvas  
  63.             width="150"  height= "150"   
  64.             x="150"  y= "0"   
  65.             backgroundColor="green" >  
  66.             <mx:Button label="I'm in Green" />  
  67.         </mx:Canvas>  
  68.         <mx:Canvas  
  69.             width="150"  height= "150"   
  70.             x="0"  y= "150"   
  71.             backgroundColor="blue" >  
  72.             <mx:Button label="I'm in Blue" />  
  73.         </mx:Canvas>  
  74.         <mx:Canvas  
  75.             width="150"  height= "150"   
  76.             x="150"  y= "150"   
  77.             backgroundColor="magenta" >  
  78.             <mx:Button label="I'm in Magenta" />  
  79.         </mx:Canvas>  
  80.     </mx:Canvas>  
  81. </mx:Application> 

执行效果图如下:


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值