<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()"> <mx:Script> <!--[CDATA[ import mx.core.DragSource; import mx.events.DragEvent; import flash.events.MouseEvent; import mx.managers.DragManager; [Embed("pic/akeayu.gif")] [Bindable] public var First:Class; [Embed("pic/goggles.gif")] [Bindable] public var Second:Class; internal function initApp():void{ img.doubleClickEnabled = true; img.addEventListener(MouseEvent.DOUBLE_CLICK,onClick); } private function onClick(event:MouseEvent):void{ var newImage:Image=new Image(); newImage.source = event.currentTarget.source; newImage.x = 50; newImage.y = 50; cav2.addChild(newImage); newImage.doubleClickEnabled = true; newImage.addEventListener(MouseEvent.DOUBLE_CLICK,doDelete); } private function doDelete(event:MouseEvent):void{ var image:Image = Image(event.currentTarget); image.visible = false; } /*下面是添加拖拽效果*/ //拖拽开始 private function mouseMoveHandler(event:MouseEvent):void{ var dragInitiator:Image = Image(event.currentTarget); var ds:DragSource = new DragSource(); ds.addData(dragInitiator,'img'); DragManager.doDrag(dragInitiator,ds,event); } //添加dragEnter private function dragEnterHandler(event:DragEvent):void{ if(event.dragSource.hasFormat('img')){ DragManager.acceptDragDrop(Canvas(event.currentTarget)); } } //添加dragDrop private function dragDropHandler(event:DragEvent):void{ if(event.dragSource.hasFormat('img')){ var draggedImage:Image = event.dragSource.dataForFormat('img') as Image; var dropCanvas:Canvas = event.currentTarget as Canvas; var newImage:Image = new Image(); newImage.source = draggedImage.source; newImage.x = event.localX; newImage.y = event.localY; cav2.addChild(newImage); newImage.doubleClickEnabled = true; newImage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler); newImage.addEventListener(MouseEvent.DOUBLE_CLICK,doDelete); newImage.addEventListener(DragEvent.DRAG_COMPLETE,dragCompleteHandler); } } //拖拽dragComplete private function dragCompleteHandler(event:DragEvent):void { var draggedImage:Image = event.dragInitiator as Image; var dragInitCanvas:Canvas = event.dragInitiator.parent as Canvas; if (event.action == DragManager.MOVE) dragInitCanvas.removeChild(draggedImage); } /*添加图像缩放*/ public function doZoom(event:MouseEvent):void { if (zoomAll.isPlaying) { zoomAll.reverse(); } else { zoomAll.play([event.target], event.type == MouseEvent.ROLL_OUT ? true : false); } } private function smoothImage(ev:Event):void{ var bmp:Bitmap = ev.target.content as Bitmap; bmp.smoothing = true; } ]]--> </mx:Script> <mx:Zoom id="zoomAll" zoomWidthTo="1" zoomHeightTo="1" zoomWidthFrom=".5" zoomHeightFrom=".5" /> <mx:Canvas id="cav1" x="33" y="20" width="200" height="323" backgroundColor="#FFFFFF"> <mx:Image id="img" x="10" y="10" source="{First}" mouseMove="mouseMoveHandler(event);" rollOver="doZoom(event)" rollOut="doZoom(event)" creationComplete="smoothImage(event);" /> <mx:Image x="68" y="10" source="{Second}" height="59" mouseMove="mouseMoveHandler(event)" rollOver="doZoom(event)" rollOut="doZoom(event)" creationComplete="smoothImage(event);" /> </mx:Canvas> <mx:Canvas id="cav2" x="396" y="20" width="200" height="323" backgroundColor="#FFFFFF" dragEnter="dragEnterHandler(event);" dragDrop="dragDropHandler(event);"> </mx:Canvas> </mx:Application> /本篇文章来源于ACCP教程网,原文出处:http://www.accptech.com/article/flex/1697.html