AIR内部的拖拽。其实,这个用到了前边两种拖拽里的东东。用到了创建剪贴板,启动拖动(拖出),用到了接受剪贴板(拖入)。
会创建剪贴板,处理事件,了解流程,这种拖拽就很好实现了。AIR内部拖拽,还有一个权力,可以自定义拖拽对应数据类型。命名的时候不要以flash:或air:开头就好。
在拖拽的过程中,鼠标状态的变换,追随鼠标图片的坐标,大小等细节方面也是要注意的。
package { import flash.desktop.Clipboard; import flash.desktop.ClipboardFormats; import flash.desktop.NativeDragManager; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.NativeDragEvent; import flash.geom.Matrix; import flash.geom.Point; import flash.net.URLRequest; [SWF(width = "450",height = "720",frameRate = "30")] public class DragDemo extends Sprite { private var receivePanel:Sprite; private var sendPanel:Sprite; private var bitmap:Bitmap; private var bitmapData:BitmapData; public function DragDemo():void { sendPanel = new Sprite(); addChild(sendPanel); receivePanel = new Sprite(); receivePanel.graphics.beginFill(0x00ffff,0.25); receivePanel.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight * 0.2); receivePanel.graphics.endFill(); addChild(receivePanel); receivePanel.y = stage.stageHeight * (1 - 0.2); readyReceiveThing(); readyDragThing(); } private function readyReceiveThing():void { receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,dragEnterHandler); receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,dragDropHandler); receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_EXIT,dragExitHandler); receivePanel.addEventListener(MouseEvent.CLICK,clickHandler); } private function dragEnterHandler(e:NativeDragEvent):void { var clipBoard:Clipboard = e.clipboard; if(clipBoard.hasFormat(ClipboardFormats.BITMAP_FORMAT)) { NativeDragManager.acceptDragDrop(receivePanel); } } private function dragDropHandler(e:NativeDragEvent):void { var receiveBitmapdata:BitmapData = e.clipboard.getData(ClipboardFormats.BITMAP_FORMAT) as BitmapData; var receiveBitmap:Bitmap = new Bitmap(); receiveBitmap.bitmapData = receiveBitmapdata; var rceiveBitmapScale:Number = receivePanel.height/receiveBitmap.height; receiveBitmap.scaleX = rceiveBitmapScale; receiveBitmap.scaleY = rceiveBitmapScale; receivePanel.addChild(receiveBitmap); receiveBitmap.x = (receivePanel.numChildren -1) * receiveBitmap.width; } private function clickHandler(e:MouseEvent):void { if(receivePanel.numChildren>0) { var _delBitmap:Bitmap = receivePanel.getChildAt((receivePanel.numChildren -1)) as Bitmap; receivePanel.removeChild(_delBitmap); _delBitmap.bitmapData.dispose(); } } private function dragExitHandler(e:NativeDragEvent):void { } private function readyDragThing():void { var myLoader:Loader = new Loader(); var myRequest:URLRequest = new URLRequest(); myRequest.url ="http://img.zcool.cn/community/01be3a554344dc0000002b01013da7.jpg"; myLoader.load(myRequest); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); } private function completeHandler(e:Event):void { e.target.removeEventListener(Event.COMPLETE,completeHandler); bitmap = e.target.content as Bitmap; sendPanel.addChild(bitmap); bitmap.width = stage.stageWidth; bitmap.scaleY = bitmap.scaleX; sendPanel.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler); } private function mouseDownHandler(e:MouseEvent):void { var clipboard:Clipboard = new Clipboard(); clipboard.setData(ClipboardFormats.BITMAP_FORMAT,bitmap.bitmapData,true); if(bitmapData) { bitmapData.dispose(); } bitmapData = new BitmapData(bitmap.width,bitmap.height,true,0); var matrix:Matrix = new Matrix(); matrix.scale(bitmap.scaleX,bitmap.scaleY); bitmapData.draw(bitmap,matrix); NativeDragManager.doDrag(receivePanel,clipboard,bitmapData,new Point(-mouseX,-mouseY)); } } }