AIR拖拽的三步骤:启动,拖动,放下。
启动的时候,需要借助鼠标事件。由NativeDragManager控制。在哪个阶段,有事件通知。任何 InteractiveObject 类型的对象都可以是拖动启动器或放置目标。
三种类型的拖动,直接看code。
1,从操作系统(包裹操作系统里的应用程序)拖拽。
准备:1,先绘制一个Sprite,用来接受拖拽的数据。这个时候,就得对这个绘制Sprite进行事件侦听。
receivePanel = getPanel(stage.stageWidth , stage.stageHeight , 0xff0099 , 0.0);
addChild(receivePanel);
receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER , dragEnterHandler);
receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP , dragDropHandler);
NATIVE_DRAG_ENTER,当拖拽的对象进入Sprite时候,就开始触发了。这个时候你可以检测剪贴板(Clipboard)所包含的对应数据类型,然后是否启用接受。启用接受请用NativeDragManager。
NativeDragManager.acceptDragDrop(receivePanel);
当拖拽对象在Sprite上,松开鼠标,停止拖拽时,抛给你的是NATIVE_DRAG_DROP事件。到此,你已经接受到数据了。然后处理数据。从操作系统拖拽文件(数据)到AIR中,就这么完成了。其实,还有其他事件,只是没写进来。比如NATIVE_DRAG_OVER,NATIVE_DRAG_COMPLETE等。下边贴出完整的代码。
package { import flash.desktop.Clipboard; import flash.desktop.ClipboardFormats; import flash.desktop.NativeDragManager; import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; import flash.events.NativeDragEvent; [SWF(width = "720" , height="450" , frameRate="30")] public class DragFileToAIR extends Sprite { private var receivePanel:Sprite; private var textArea:TextArea; private var bitmap:Bitmap; public function DragFileToAIR() { stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; stage.addEventListener(Event.RESIZE , resizeHandler); initialize(); } private function initialize():void { textArea = new TextArea("微软雅黑" , 12 , 0xff0000 , stage.stageWidth - 16 , stage.stageHeight - 16 ); addChild(textArea); textArea.x = textArea.y = 8; bitmap = new Bitmap(); addChild(bitmap); //拖动对象进入,第一个触发的事件。不必dragOver,dragOver只要拖动对象进入会一直触发。这个只在进入的刹那触发一次。 //这个时候可以检查clipboard的数据释放合适。选择是否接受。这个时候,拖动图形状态还是禁止拖入状态。 receivePanel = getPanel(stage.stageWidth , stage.stageHeight , 0xff0099 , 0.0); addChild(receivePanel); receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER , dragEnterHandler); receivePanel.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP , dragDropHandler); } /** * * @param e * 这里,你可以过滤你想要的剪贴内容。你也可以不过滤。 */ private function dragEnterHandler(e:NativeDragEvent):void { var clipboard:Clipboard = e.clipboard; if(clipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT) || clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT) || clipboard.hasFormat(ClipboardFormats.HTML_FORMAT) || clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) { NativeDragManager.acceptDragDrop(receivePanel); } } private function dragDropHandler(e:NativeDragEvent):void { var clipboard:Clipboard = e.clipboard; if(clipboard.hasFormat(ClipboardFormats.BITMAP_FORMAT)) { var bitmapData:BitmapData = clipboard.getData(ClipboardFormats.BITMAP_FORMAT) as BitmapData; echo("拖入了一张位图!"); if(bitmap && bitmap.bitmapData) { bitmap.scaleX = bitmap.scaleY = 1; bitmap.bitmapData.dispose(); } bitmap.bitmapData = bitmapData; resizeBitmap(); } if(clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT)) { var fileList:Array = clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array; echo("拖入了一个文件列表,一共有" + fileList.length + "个文件!"); } if(clipboard.hasFormat(ClipboardFormats.HTML_FORMAT)) { var htmlStr:String = clipboard.getData(ClipboardFormats.HTML_FORMAT) as String; echo("拖入了一个html格式文件!"); } if(clipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) { var textStr:String = clipboard.getData(ClipboardFormats.TEXT_FORMAT) as String; echo("拖入了一个无格式文本文件!"); } echo("\n"); clipboard.clear(); } private function echo(value:String):void { textArea.text = value; } private function resizeHandler(e:Event):void { if(receivePanel) { receivePanel.width = stage.stageWidth; receivePanel.height = stage.stageHeight; } if(textArea) { textArea.x = stage.stageWidth * 0.5 - textArea.width * 0.5; textArea.y = stage.stageHeight * 0.5 - textArea.height * 0.5; } resizeBitmapPos(); } private function resizeBitmapPos():void { if(bitmap) { bitmap.x = stage.stageWidth * 0.5 - bitmap.width * 0.5; bitmap.y = stage.stageHeight * 0.5 - bitmap.height * 0.5; } } private function resizeBitmap():void { if(bitmap && bitmap.bitmapData) { if(bitmap.width/bitmap.height > (textArea.width - 3)/(textArea.height - 3)) { bitmap.width = textArea.width - 3; bitmap.scaleY = bitmap.scaleX; } else { bitmap.height = textArea.height - 3; bitmap.scaleX = bitmap.scaleY; } resizeBitmapPos(); } } private function getPanel(w:int , h:int , color:int , alp:Number = 0.5):Sprite { var sp:Sprite = new Sprite(); sp.graphics.beginFill(color , alp); sp.graphics.drawRect(0 , 0 , w , h); sp.graphics.endFill(); return sp; } } } import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; class TextArea extends Sprite { private var textField:TextField; private var textWidth:int; private var textHeight:int; private const PADDING:int = 8; public function TextArea(font:String , size:int , color:int , tw:int , th:int , bold:Boolean = false) { textField = new TextField(); textField.autoSize = TextFieldAutoSize.LEFT; textField.defaultTextFormat = new TextFormat(font , size , color , bold); textField.wordWrap = true; textField.multiline = true; textField.width = tw - PADDING * 2; textField.height = th - PADDING * 2; addChild(textField); textField.x = PADDING; textField.y = PADDING; textWidth = tw; textHeight = th; mouseChildren = false; mouseEnabled = false; graphics.clear(); graphics.lineStyle(2 , color ); graphics.beginFill(color , 0); graphics.drawRect(0 , 0 , tw , th); graphics.endFill(); } public function set text(value:String):void { textField.appendText(value + "\n"); if(textField.height > (height - PADDING * 2)) { textField.text = value; } } override public function get width():Number { return textWidth; } override public function get height():Number { return textHeight; } }