[AIR]如何將應用程式上的圖片複製在OS的桌面上
前面介紹過將圖片拖曳進應用程式上,當然也要來研究一下如果我們想將應用程式裡的圖片也copy一份出來時,該怎麼進行呢!在 AIR 中,Clipboard 類別用於拖放作業上。Clipboard 類別是提供透過剪貼簿來傳輸資料與物件所需的容器。所以此範例也會用到Clipboard類別裡的方法來完成我們的需求。
製作觀念與語法解析
將圖片拖曳到視窗外變成圖片顯示在桌面上,這裡有兩個地方要注意:
第一個就是先將圖片註冊mouseDown的事件,當滑鼠確定點選到該圖檔時,要將點選的圖片先轉成Bitmap的Object形態,暫存在Clipboard容器上。
其實在Help文件也說得挺清楚
Starts a drag-and-drop operation.
To start a drag operation:
1.Create a new Clipboard object.
2.Add the data to be transferred in one or more formats.
3.Optionally, create a BitmapData object to serve as a proxy image during the drag.
4.Optionally, create a NativeDragOptions object to restrict the actions allowed in this operation. (If the allowedActions parameter is left null, all actions are allowed.)
5.Call NativeDragManager.doDrag().讓拖曳可以啟動需有以下的操作。
第二個將點選的圖片先轉成Bitmap的Object形態,暫存在Clipboard容器上,在此需要另外一個method去處理。
這裡會用到Clipboard類別裡的setData方法,加入要以指定資料格式進行傳輸的資訊形式,將資料形態轉換,把物件轉成BitmapData的type。
setData(format:String, data:Object, serializable:Boolean = true):Boolean
1.format:String — 資料的格式。
2.data:Object — 要加入的資訊。
底下秀出實際操作過程的一些圖片:
放置一張圖片在應用程式裡。
將圖片拖曳到資料夾,可發覺自動存成圖片檔案了。
DragOut.mxml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import flash.filesystem.File; private function onMouseDown(event:MouseEvent):void { var bitmap:Bitmap = Bitmap(event.target.content); var bitmapFile:File = new File(event.target.content.loaderInfo.url); var transferObject:Clipboard = createTransferableData(bitmap,bitmapFile); NativeDragManager.doDrag(this,transferObject,bitmap.bitmapData, new Point(-mouseX,- mouseY)); } private function createTransferableData(image:Bitmap, sourceFile:File):Clipboard { var transfer:Clipboard = new Clipboard(); transfer.setData(ClipboardFormats.BITMAP_FORMAT,image.bitmapData,false); transfer.setData(ClipboardFormats.BITMAP_FORMAT,image.bitmapData,false); transfer.setData(ClipboardFormats.FILE_LIST_FORMAT,new Array(sourceFile), false); return transfer; } ]]> </mx:Script> <mx:Image id="image" source="earth.gif" mouseDown="onMouseDown(event)" x="132" y="73"/> </mx:WindowedApplication> |
沒有相關文章.



近期迴響