图片拖放的实现(drag and drop)

  1. backgroundColor= "#333333" xmlns:tools= "tools.*" creationComplete= "init();"
  2. >
  3.  
  4. <!-- [CDATA [
  5. import mx. core. UIComponent;
  6. import mx. core. DragSource;
  7. import mx. events. DragEvent;
  8. import mx. managers. DragManager;
  9.  
  10. [Embed (source= "images/aoao_120.png" ) ]-->      private var imgsrc: Class;
  11.  
  12. private function init ( ): void
  13. {
  14. img1. source = imgsrc;
  15. img2. source = imgsrc;
  16. }
  17.  
  18. /**
  19. * 开始拖拽
  20. * */
  21. private function dragHandler (event:MouseEvent ): void
  22. {
  23. // 定义拖拽挂载数据源
  24. var ds:DragSource = new DragSource ( );
  25. // 定义拖拽图标(用于第二副图片的拖拽)
  26. var di:Image = new Image ( );
  27. // 拖拽对象
  28. var target:Image = event. currentTarget as Image;
  29.  
  30. // 添加拖拽数据源数据
  31. ds. addData ( target, "img" );
  32. ds. addData (event. localX, "x" );
  33. ds. addData (event. localY, "y" );
  34.  
  35. // 拖拽图标
  36. if ( target. id == "img2" )
  37. di. source = new Bitmap (getBitmapData ( target. content ) );
  38. else
  39. di = null;
  40. // 执行拖拽
  41. DragManager. doDrag ( target, ds, event, di );
  42. }
  43. /**
  44. * 进入拖放区域
  45. * */
  46. private function dragEnterHandler (event:DragEvent ): void
  47. {
  48. // 进入区域, 设置状态为允许拖放
  49. if (event. dragSource. hasFormat ( "img" ) )
  50. DragManager. acceptDragDrop (event. target as UIComponent );
  51. }
  52. /**
  53. * 释放拖拽
  54. * */
  55. private function dragDropHandler (event:DragEvent ): void
  56. {
  57. // 复制图片
  58. var img:Image = new Image ( );
  59. img. source = new Bitmap (getBitmapData (event. dragSource. dataForFormat ( "img" ) as DisplayObject ) );
  60. // 处理图片防止位置
  61. img. x = event. currentTarget. mouseX - (event. dragSource. dataForFormat ( "x" ) as Number );
  62. img. y = event. currentTarget. mouseY - (event. dragSource. dataForFormat ( "y" ) as Number );
  63. // 显示图片
  64. event. currentTarget. addChild (img );
  65. }
  66. /**
  67. * 获取图片信息
  68. * */
  69. private function getBitmapData ( target:DisplayObject ):BitmapData
  70. {
  71. var res:BitmapData = new BitmapData ( target. width, target. height );
  72. res. draw ( target );
  73. return res;
  74. }
  75. ] ]&gt;
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. dragEnter= "dragEnterHandler(event);" dragDrop= "dragDropHandler(event);" /&gt;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值