拖拉图片2

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<s:layout>
<s:BasicLayout/>
</s:layout>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private var oldX:Number; //拖动开始是的坐标
private var oldY:Number;

//拖动开始
private function dragStart(event:MouseEvent):void
{
oldX=event.stageX; //相对Application的坐标
oldY=event.stageY;
}

//拖动结束
private function dragEnd(event:MouseEvent):void
{
lbl.text="Local (x,y):" + event.localX.toString() + "," + event.localX.toString(); //相对图片的坐标
lbl2.text="Stage (x,y):" + event.stageX.toString() + "," + event.stageY.toString(); //相对Application的坐标
if (event.buttonDown)
{ //如果鼠标在移动过程中并且按下,也就是说鼠标的拖动事件。
var x:Number=event.stageX - oldX; //相对Application的坐标的移动量
var y:Number=event.stageY - oldY;
oldX=event.stageX; //更新拖动开始是的坐标
oldY=event.stageY;
img.move(img.x + x, img.y + y); //img是 Image的id,如果扩展到其他组件,改这里就可以了。
}
}
]]>
</fx:Script>
<mx:Label x="10"
y="10"
text=""
id="lbl"/>
<mx:Label x="10"
y="27"
text=""
id="lbl2"/>
<mx:Panel x="257"
y="71"
width="700"
height="80%"
layout="absolute"
horizontalScrollPolicy="off"
verticalScrollPolicy="off"
title="图片在面板中的拖动例子"
fontSize="12">
<mx:Image id="img"
x="0"
y="0"
source="test.JPG"
mouseMove="dragEnd(event)"
mouseDown="dragStart(event)"/>
</mx:Panel>

</s:Application>

posted @ 2014-03-24 21:24  Brook Legend  阅读(127)  评论(0编辑  收藏  举报