:: 首页 :: 新随笔 ::  :: 聚合  :: 管理

图形拖动(一)副本的方式

Posted on 2012-10-20 14:40 谁用我名字啦? 阅读(181) 评论(0)  编辑  收藏 所属分类: flex学习之路
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s
="library://ns.adobe.com/flex/spark" 
               xmlns:mx
="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
    
<fx:Script>
        
<![CDATA[
            import mx.controls.*;
            import mx.core.IVisualElement;
            import mx.core.UIComponent;
            import mx.graphics.ImageSnapshot;  
            
            [Embed(source="E:/flex/45.png")]
            [Bindable]
            private var bookImage:Class;
            
            public var uiComponent:Object;
            public var targetImage:Object;
            public var Ax:int = 0;
            public var Ay:int = 0;
            public function mouseDown(event:MouseEvent):void{
                //在原地生成图片副本,然后将移动图片。  
                this.uiComponent = event.currentTarget;
                //生成图片  
                var bd:BitmapData = ImageSnapshot.captureBitmapData( UIComponent( uiComponent ) );  
                targetImage = new Image();   
                targetImage.source = new Bitmap(bd);  
                targetImage.x = uiComponent.x;  
                targetImage.y = uiComponent.y;  
                Ax = event.localX;  
                Ay = event.localY;  
                this.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);  
                this.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
                this.addElement(targetImage as IVisualElement);// as DisplayObject
            }  
            
            public function mouseMove(event:MouseEvent):void{  
                targetImage.x = event.stageX-Ax;  
                targetImage.y = event.stageY-Ay;  
            }  
            
            public function mouseUp(event:MouseEvent):void{  
                this.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);  
                this.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);  
                
                //当鼠标移动到预定位置,将图片删除,并且移动源控件。  
                uiComponent.x = event.stageX-Ax;  
                uiComponent.y = event.stageY-Ay;  
                this.removeElement(targetImage as IVisualElement);  
                targetImage = null;  
                
            }  
        
]]>
    
</fx:Script>
    
<mx:Image x="331" y="127" source="{bookImage}" mouseDown="mouseDown(event)"/>
    
</s:Application>

只有注册用户登录后才能发表评论。


网站导航: