Flex坐标系统及转换

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


          全局的就是(stage级别的):全局舞台的坐标,不随当前当鼠标所在容器的变化而变化

          本地坐标系(组件级别的):随控件不是定是容器的变化而坐标系变化,显示的是当前构件内的坐标

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

    这些坐标系的点是可以转换的,并且有相应的方法,看来adobe想得挺周到。我们一个一个的说一下:
    全局
           这个坐标系的原点在整个flash舞台的左上角,MouseEvent实例的stageX,stageY就是这个坐标系中的值。

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

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

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

注:x,y属性始终是指显示对象相对于父显示对象坐标的(0,0)坐标的位置。
本地---显示对象
全局---舞台
坐标转换还有现成的方法:

contentMouseX  返回mouse的内容坐标x值

contentMouseY  返回mouse的内容坐标Y值

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

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

globalToContent     将全局的转成内容坐标

globalToLocal  全局的转成本地的
globalToLocal(pointoint)oint将 point 对象从舞台(全局)坐标转换为显示对象的(本地)坐标。 该对象具有相对于显示对象原点(而不是舞台原点)的 x 和 y 值。
返回相对于显示对象原点的值。                 
gtol = 全局舞台中的本地坐标,而不是相对于父容器的坐标 (-x,-y) + point(x,y)

localToContent 本地到内容坐标


localToGlobal  本地到全局坐标
localToGlobal(pointoint):Point将 point 对象从显示对象的(本地)坐标转换为舞台(全局)坐标。
返回相对于舞台原点的值。
ltog = 全局舞台中的本地坐标,而不是相对于父容器的坐标 (x,y) + point(x,y)



下面是一个小例子:

<?xml version="1.0"?>  
<!-- containers\intro\MousePosition.mxml -->  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"  
    backgroundColor="white">  
     
    <mx:Script>  
        <!--[CDATA[  
          import mx.controls.Alert;  
          // Handle the mouseDown event generated  
          // by clicking in the application.  
          private function handleMouseDown(event:MouseEvent):void {  
                 
            // Convert the mouse position to global coordinates.  
            // The localX and localY properties of the mouse event contain  
            // the coordinates at which the event occurred relative to the  
            // event target, typically one of the  
            // colored internal Canvas controls.  
            // A production version of this example could use the stageX  
            // and stageY properties, which use the global coordinates,  
            // and avoid this step.  
            // This example uses the localX and localY properties only to  
            // illustrate conversion between different frames of reference.  
            var pt:Point = new Point(event.localX, event.localY);  
            pt = event.target.localToGlobal(pt);  
                 
            // Convert the global coordinates to the content coordinates  
            // inside the outer c1 Canvas control.  
            pt = c1.globalToContent(pt);  
                
            // Figure out which quadrant was clicked.  
            var whichColor:String = "border area";  
               
            if (pt.x < 150) {  
                if (pt.y < 150)  
                    whichColor = "red";  
                else  
                    whichColor = "blue";  
            }  
            else {  
                if (pt.y < 150)  
                    whichColor = "green";  
                else  
                    whichColor = "magenta";  
            }  
                 
            Alert.show("You clicked on the " + whichColor);  
          }  
        ]]-->  
    </mx:Script>  
    <!-- Canvas container with four child Canvas containers -->  
    <mx:Canvas id="c1"  
        borderStyle="none"  
        width="300" height="300"  
        mouseDown="handleMouseDown(event);">  
         
        <mx:Canvas  
            width="150" height="150"  
            x="0" y="0"  
            backgroundColor="red">  
            <mx:Button label="I'm in Red"/>  
        </mx:Canvas>  
        <mx:Canvas  
            width="150" height="150"  
            x="150" y="0"  
            backgroundColor="green">  
            <mx:Button label="I'm in Green"/>  
        </mx:Canvas>  
        <mx:Canvas  
            width="150" height="150"  
            x="0" y="150"  
            backgroundColor="blue">  
            <mx:Button label="I'm in Blue"/>  
        </mx:Canvas>  
        <mx:Canvas  
            width="150" height="150"  
            x="150" y="150"  
            backgroundColor="magenta">  
            <mx:Button label="I'm in Magenta"/>  
        </mx:Canvas>  
    </mx:Canvas>  
</mx:Application>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值