flex 開發卡片照片編輯篇(二)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" 
  3.     width="600" 
  4.     height="510" 
  5.     creationComplete="initUI();" 
  6.     backgroundColor="#E6E6E6"  
  7.     verticalScrollPolicy="off" 
  8.     horizontalScrollPolicy="off"  
  9.     borderColor="#ACA5A5" 
  10.     borderStyle="solid" 
  11.     borderThickness="2"  
  12.     color="#AD4B40">
  13. <mx:Script> 
  14.         <![CDATA[
  15.         import mx.controls.Image;
  16.         import code.ProductHandlerBasic;
  17.         import mx.core.BitmapAsset;
  18.         import mx.controls.Label; 
  19.         import mx.core.Container; 
  20.         import flash.geom.Rectangle ;  
  21.         import mx.managers.PopUpManager;  
  22.         import mx.managers.PopUpManagerChildList 
  23.         
  24.         /*裁剪框*/
  25.         private var _rectBorder:Sprite ;
  26.            
  27.         /*裁剪拉伸光標*/
  28.         private var _cursor:Image ;
  29.         
  30.         /*裁剪框的座標寬度數據*/ 
  31.         private var _w:Number = 0;
  32.         private var _h:Number = 0 ; 
  33.         private var _x:Number = 0 ;
  34.         private var _y:Number = 0 ;
  35.         private var _url:String ;
  36.         
  37.         
  38.         private var mouseState:int = 0 ;//鼠標狀態,0 爲正常 1:爲按下
  39.          
  40.         /*臨時存放裁剪框座標大小信息*/
  41.         private var _tempObj:Object = new Object() ; 
  42.         
  43.         /*存放鼠標座標減去裁剪邊框的座標*/ 
  44.         private var _mx:Number ; 
  45.         private var _my:Number ; 
  46.         
  47.         /*模板圖片框的顯示比例.根據此比例才能算出裁剪框的大小*/ 
  48.         private var _targeimgtscale:Number = 1 ;
  49.         
  50.         /*模板上的圖片對象.*/
  51.         private var _targetimg:Image ; //版式上的圖片 
  52.         
  53.         //光標是否在左邊界和右邊界的標識 
  54.         private var _criticalFlag:Number ;
  55.         
  56.         private var _p:ProductHandlerBasic ;
  57.         /*當前圖片相對應的bitmapasset對象*/
  58.         private var _currentEditImg:Image ;
  59.         
  60.         [Embed(source="assets/03.gif")] 
  61.         private var flag:Class;
  62.          
  63.     
  64.         private function initUI():void{
  65.         }
  66.         public function set producthandlerBase(pro_:ProductHandlerBasic):void{
  67.             this._p = pro_ ;
  68.         } 
  69.         public function get producthandlerBase():ProductHandlerBasic{
  70.             return this._p ;
  71.         }
  72.         
  73.         /**
  74.          * 初始化照片編輯所需要的參數 。
  75.          * @param _img :所要編輯的圖片Bitmap對像。模板框裏的圖片 。
  76.          * @param _url :所要編輯的圖片url。由於_img是生成的bitmap而的不到source.所以再傳
  77.          * @param _x   :裁剪框在編輯圖片中的x ;
  78.          * @param -y   :................y
  79.          */
  80.         public function initphotoEditParams(img_t:Image,url_t:String,x_t:Number,y_t:Number,w_t:Number,h_t:Number):void{
  81.             this._targeimgtscale = img_t.width/img_t.height ; 
  82.             this._targetimg = img_t ; 
  83.             this._x = x_t ;
  84.             this._y = y_t ;
  85.             this._w = w_t ; 
  86.             this._h = h_t ;
  87.             this._url = url_t ; 
  88.             var request_t:URLRequest = new URLRequest(url_t);
  89.             var loader_t:Loader = new Loader ();
  90.             loader_t.load(request_t) ;
  91.             loader_t.contentLoaderInfo.addEventListener(Event.COMPLETE,photoLoaderCompleteHandler) ;
  92.         }
  93.         /**所編輯圖片加載完成處理*/ 
  94.         private function photoLoaderCompleteHandler(e:Event):void{
  95.             var loader_t:Loader = e.currentTarget.loader as Loader ;
  96.             var asset_:Bitmap = new Bitmap() ;
  97.             var bitmapdata_t:BitmapData = new BitmapData(loader_t.width,loader_t.height);
  98.             bitmapdata_t.draw(loader_t);
  99.             asset_.bitmapData = bitmapdata_t ;
  100.             /*創建當前需要編輯的用戶圖像*/
  101.             _currentEditImg = new Image() ;
  102.             _currentEditImg.source = asset_ ;
  103.             _currentEditImg.width = bitmapdata_t.width ;
  104.             _currentEditImg.height= bitmapdata_t.height ;
  105.              
  106.             _currentEditImg.x = (this.width-menuID.width-loader_t.width)/2 ;
  107.             _currentEditImg.y = (this.height-loader_t.height)/2 ; 
  108.              
  109.             this.addChild(_currentEditImg) ;
  110.             /*創建紅色矩形裁剪框*/
  111.              _rectBorder = new Sprite() ; 
  112.              _rectBorder.x = this._x ;
  113.              _rectBorder.y = this._y ;
  114.             /*設置裁剪區域邊框樣式*/      
  115.             _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
  116.             _rectBorder.graphics.beginFill(0x330000,0);            
  117.             _rectBorder.graphics.drawRect(0,0,this._w,this._h);     
  118.             _rectBorder.graphics.endFill();  
  119.             this._currentEditImg.addChild(_rectBorder); 
  120.             /*創建光標對象*/
  121.             _cursor = new Image(); 
  122.             /*一定要用這種方式給光標傳值,否則會初相亂七八糟的效果。真他媽的鬱悶,搞了好幾天才發現,估計是flex的bug*/
  123.             _cursor.source = flag ;
  124.             _cursor.width =  30 ;
  125.             _cursor.height = 30 ;
  126.             _cursor.visible = false ;
  127.             
  128.             this._currentEditImg.addChild(_cursor); 
  129.             this._currentEditImg.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
  130.             this._currentEditImg.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler) ;
  131.             this._currentEditImg.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
  132.             this._currentEditImg.addEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler) ;
  133.             this._currentEditImg.doubleClickEnabled = true ;
  134.         }
  135.         /**
  136.          * 雙擊應用修改
  137.          */ 
  138.         private function doubleClickHandler(e:MouseEvent):void{
  139.             this.applicationHandler() ;
  140.         }  
  141.         /**
  142.          * 鼠標按下
  143.          */
  144.         private function mouseDownHandler(e:MouseEvent):void{
  145.             this._tempObj.x = _rectBorder.x ;
  146.             this._tempObj.y = _rectBorder.y ;
  147.             this._tempObj.width = this._w ;
  148.             this._tempObj.height = this._h ;
  149.             this.mouseState = 1 ;
  150.             
  151.         }
  152.         /**
  153.          * 鼠標彈起
  154.          */
  155.         private function mouseUpHandler(e:MouseEvent):void{
  156.              this.mouseState = 0 ;
  157.              this._rectBorder.stopDrag() ; 
  158.         } 
  159.          
  160.         /**
  161.          * 鼠標移動及拖動處理
  162.          */ 
  163.         private function mouseMoveHandler(e:MouseEvent):void
  164.             _mx = mouseX - this._currentEditImg.x ; 
  165.             _my = mouseY - this._currentEditImg.y ;  
  166.             _cursor.x = _mx-_cursor.width/2 ;
  167.             _cursor.y =  _my - _cursor.height/2 ; 
  168.             this._cursor.visible = false ;
  169.             Mouse.show();
  170.             /*光標在左邊邊緣拖動*/
  171.            if((_criticalFlag==1) && this.mouseState == 1){
  172.                 leftDragHandler();
  173.             }   
  174.             /*光標在右邊邊緣拖動*/
  175.             else if((_criticalFlag==2)  && this.mouseState == 1){ 
  176.                 this.rightDragHandler();
  177.             }
  178.             /*光標在中間拖動*/
  179.             else if((_criticalFlag==3) && this.mouseState ==1){
  180.                 var rt:Rectangle = new Rectangle();
  181.                 rt.x = rt.y = 0 ;     
  182.                 rt.width =  _currentEditImg.width  - this._w ;
  183.                 rt.height = _currentEditImg.height - _h ; 
  184.                 this._rectBorder.startDrag(false,rt) ;
  185.                 this.mouseState = 2 ; 
  186.             }
  187.             //在左邊緣 
  188.             if(isLeftEdge()){  
  189.                 this._cursor.visible = true ;    
  190.                 Mouse.hide() ;  
  191.                 _criticalFlag =1;
  192.             } 
  193.              
  194.             else if(isRightEdge()){
  195.                 this._cursor.visible = true ;
  196.                 Mouse.hide() ;  
  197.                 _criticalFlag =2;
  198.             }else if(isInCutRect()){
  199.                  _criticalFlag = 3;  
  200.             }   
  201.             else{  
  202.                 _criticalFlag = 0 ;
  203.             }
  204.             e.updateAfterEvent() ; 
  205.         }   
  206.         
  207.         /**
  208.          * 在左邊緣上拖動處理
  209.          */ 
  210.         private function leftDragHandler():void{
  211.             _rectBorder.graphics.clear();
  212.             _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
  213.             _rectBorder.graphics.beginFill(0xCCFF00, 0);      
  214.             /*左邊邊界*/ 
  215.             _rectBorder.x = _mx < 0?0:_mx ;      
  216.             /*右邊邊界*/
  217.             _rectBorder.x = _rectBorder.x >= (_w + _tempObj.x)?(_w + _tempObj.x):_rectBorder.x ;   
  218.             /*裁剪框寬度*/
  219.             _w = _tempObj.width + _tempObj.x - _rectBorder.x ;  
  220.             /*裁剪框高度*/
  221.             _h = _w / this._targeimgtscale ;           
  222.             var h2:Number = this._currentEditImg.height - this._tempObj.y ;   
  223.             /*是否撐到最大*/
  224.             if(_h>h2){                                         
  225.                 _rectBorder.x = this._tempObj.x + this._tempObj.width -h2*this._targeimgtscale;
  226.                 _w = h2* this._targeimgtscale ;
  227.                 this._h = h2 ;  
  228.             }        
  229.             _rectBorder.graphics.drawRect(0, 0, _w, _h );  
  230.             _rectBorder.graphics.endFill();    
  231.         }
  232.         
  233.         private function rightDragHandler():void{
  234.             _rectBorder.graphics.clear();
  235.             _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
  236.             _rectBorder.graphics.beginFill(0xCCFF00, 0);   
  237.             _w= _mx > _currentEditImg.width?_currentEditImg.width:_mx ;//右邊界判斷
  238.             _w = (_w <= this._tempObj.x)?_tempObj.x:_w ;  //左邊界判斷 
  239.             _w = _w-this._tempObj.x ; //求出矩形寬度
  240.             _h = _w/ this._targeimgtscale ; //求出高度
  241.             _rectBorder.y = _tempObj.height + _tempObj.y - _h; //求出最大允許高度
  242.             if (_rectBorder.y < 0) {                       
  243.                 _h = _tempObj.height + _tempObj.y ;//最大寬度爲矩形寬度
  244.                 _rectBorder.y = 0;             
  245.                 _w =  _h * this._targeimgtscale ; //求出高度 
  246.                 
  247.             }
  248.             _rectBorder.graphics.drawRect(0, 0, _w,_h);   
  249.             _rectBorder.graphics.endFill();  
  250.         }
  251.         
  252.          
  253.         /**
  254.          * 拖動整個裁剪矩形
  255.          */ 
  256.         private function isStartMiddleDrag():Boolean{
  257.             if((_criticalFlag==3) && this.mouseState ==1){
  258.                 return true ;
  259.             }  
  260.             return false ;
  261.         }
  262.         
  263.         /**   
  264.          * 檢查光標是否在左邊界上  
  265.          * @return boolean
  266.          */                 
  267.         private function isLeftEdge():Boolean {  
  268.             /**檢查左邊界*/        
  269.              
  270.             if(_mx<0){
  271.                 return false ;
  272.             }  
  273.             if (_mx>=(this._rectBorder.x -BORDER1) &
  274.                 _mx<=(this._rectBorder.x + BORDER1) &&   
  275.                 _my>this._rectBorder.y &&     
  276.                 _my<(this._rectBorder.y +_h)) {  
  277.                     return true ;           
  278.             }          
  279.             return false ;   
  280.         } 
  281.          
  282.         /**
  283.          * 檢查光標是否在右邊界上
  284.          */  
  285.         private function isRightEdge():Boolean {
  286.             if(_mx>this._currentEditImg.width){ 
  287.                 return false;
  288.             } 
  289.             if (_mx>=(this._rectBorder.x + _w - BORDER1) && _mx<=(this._rectBorder.x+_w+BORDER1) &
  290.                 _my>this._rectBorder.y&&  _my<(this._rectBorder.y+_h)) { 
  291.                     return true ;             
  292.             }       
  293.             return false ; 
  294.         }
  295.         /**  
  296.          * 判斷光標標是否在裁剪區域裏面
  297.          * @return true 如果光標在裏面  
  298.          */          
  299.         private function isInCutRect():Boolean {   
  300.             if (_mx>(this._rectBorder.x +BORDER1) && 
  301.                 _mx<(this._rectBorder.x + _w - BORDER1) &
  302.                 _my>(this._rectBorder.y + BORDER1) &&    
  303.                 _my<(this._rectBorder.y + _h -BORDER1)) {
  304.                     return true ; 
  305.             }     
  306.             return false ;    
  307.         }  
  308.         private function applicationHandler():void{
  309.      
  310.             //照片編輯,保存座標參數  
  311.             _p.saveCurrentPageData(_p.currentpageNum,_p.currentphotoNum,_url,_rectBorder.x,_rectBorder.y,_w,_h);
  312.             _p.loadphotoToStage(_targetimg,_p.currentphotoNum,false,this._url) 
  313.             this.cancleHandler()
  314.         }     
  315.         private function cancleHandler():void{
  316.             
  317.             this._currentEditImg.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
  318.             this._currentEditImg.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
  319.             this._currentEditImg.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
  320.             this._currentEditImg.removeEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler);
  321.             PopUpManager.removePopUp(this);
  322.         } 
  323.         public  var BORDER1:Number = 10 ; 
  324.         
  325.         private function mouseOverHelp():void{
  326.             this.helphintID.visible = true ;
  327.             this.removeChild(this.helphintID);
  328.             this.addChildAt(this.helphintID,this.numChildren);
  329.         } 
  330.         private function mouserOutHelp():void{
  331.             this.helphintID.visible = false ;
  332.         } 
  333.          
  334.         ]]> 
  335. </mx:Script>   
  336.   
  337.         <mx:Canvas id="menuID" x="530" y="0" width="70" height="510" backgroundColor="#DCDCDC" borderStyle="solid" borderThickness="2">
  338.                 <mx:Button x="4" y="10" id="helpID"  mouseOver="mouseOverHelp();" label="幫助"  mouseOut="mouserOutHelp();" fontWeight="bold" fontSize="12" height="30" width="56" fillAlphas="[1.0, 1.0]" fillColors="[#333333, #333333]" borderColor="#C8C8C8" color="#FFFFFF" cornerRadius="3"/>
  339.                 <mx:Button x="4" y="464" label="取消" click="cancleHandler()" fontSize="12" fontWeight="bold" fontFamily="Verdana" height="30" color="#FFFFFF" themeColor="#0C00FF" cornerRadius="3" width="56" fillAlphas="[1.0, 1.0]" fillColors="[#004D99, #004D99]" borderColor="#C8C8C8"/>
  340.                 <mx:Button x="4" y="424" label="應用" click="applicationHandler()" fontSize="12" fontWeight="bold" color="#FFFFFF" height="30" themeColor="#0000FF" fillAlphas="[1.0, 1.0]" cornerRadius="3" alpha="1.0" width="56" fillColors="[#004D99, #004D99]" borderColor="#C8C8C8"/>
  341.         </mx:Canvas> 
  342.         <mx:Canvas   id="helphintID"  visible="false" x="{menuID.x-helphintID.width}" y="{this.helpID.y}" verticalScrollPolicy="off" horizontalScrollPolicy="off" backgroundColor="#FFFFFF" borderStyle="solid" borderThickness="3">
  343.             <mx:Text x="8" y="2" text="紅色框內爲您需要的照片內容,光標落在紅色框內時,按住鼠標左鍵任意角度拖動可移動框的位置; 光標落在紅色框左右兩條邊線時,會變成左右方向的箭頭形狀,此時按住鼠標左鍵左右拖動可等比例改變框的大小; 移動紅色框的位置或改變紅色框的大小直到取得您滿意的照片內容後,雙擊照片區域或點擊“應用”按鈕返回製作頁面; 點擊“取消”按鈕也可返回製作頁面,且不會對照片的位置和大小做任何改變。" width="292" height="220" fontSize="12" fontWeight="normal" color="#454545"/>
  344.         </mx:Canvas>
  345.      
  346.        
  347. </mx:Canvas>
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章