- <?xml version="1.0" encoding="utf-8"?>
- <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"
- width="600"
- height="510"
- creationComplete="initUI();"
- backgroundColor="#E6E6E6"
- verticalScrollPolicy="off"
- horizontalScrollPolicy="off"
- borderColor="#ACA5A5"
- borderStyle="solid"
- borderThickness="2"
- color="#AD4B40">
- <mx:Script>
- <![CDATA[
- import mx.controls.Image;
- import code.ProductHandlerBasic;
- import mx.core.BitmapAsset;
- import mx.controls.Label;
- import mx.core.Container;
- import flash.geom.Rectangle ;
- import mx.managers.PopUpManager;
- import mx.managers.PopUpManagerChildList
- /*裁剪框*/
- private var _rectBorder:Sprite ;
- /*裁剪拉伸光標*/
- private var _cursor:Image ;
- /*裁剪框的座標寬度數據*/
- private var _w:Number = 0;
- private var _h:Number = 0 ;
- private var _x:Number = 0 ;
- private var _y:Number = 0 ;
- private var _url:String ;
- private var mouseState:int = 0 ;//鼠標狀態,0 爲正常 1:爲按下
- /*臨時存放裁剪框座標大小信息*/
- private var _tempObj:Object = new Object() ;
- /*存放鼠標座標減去裁剪邊框的座標*/
- private var _mx:Number ;
- private var _my:Number ;
- /*模板圖片框的顯示比例.根據此比例才能算出裁剪框的大小*/
- private var _targeimgtscale:Number = 1 ;
- /*模板上的圖片對象.*/
- private var _targetimg:Image ; //版式上的圖片
- //光標是否在左邊界和右邊界的標識
- private var _criticalFlag:Number ;
- private var _p:ProductHandlerBasic ;
- /*當前圖片相對應的bitmapasset對象*/
- private var _currentEditImg:Image ;
- [Embed(source="assets/03.gif")]
- private var flag:Class;
- private function initUI():void{
- }
- public function set producthandlerBase(pro_:ProductHandlerBasic):void{
- this._p = pro_ ;
- }
- public function get producthandlerBase():ProductHandlerBasic{
- return this._p ;
- }
- /**
- * 初始化照片編輯所需要的參數 。
- * @param _img :所要編輯的圖片Bitmap對像。模板框裏的圖片 。
- * @param _url :所要編輯的圖片url。由於_img是生成的bitmap而的不到source.所以再傳
- * @param _x :裁剪框在編輯圖片中的x ;
- * @param -y :................y
- */
- public function initphotoEditParams(img_t:Image,url_t:String,x_t:Number,y_t:Number,w_t:Number,h_t:Number):void{
- this._targeimgtscale = img_t.width/img_t.height ;
- this._targetimg = img_t ;
- this._x = x_t ;
- this._y = y_t ;
- this._w = w_t ;
- this._h = h_t ;
- this._url = url_t ;
- var request_t:URLRequest = new URLRequest(url_t);
- var loader_t:Loader = new Loader ();
- loader_t.load(request_t) ;
- loader_t.contentLoaderInfo.addEventListener(Event.COMPLETE,photoLoaderCompleteHandler) ;
- }
- /**所編輯圖片加載完成處理*/
- private function photoLoaderCompleteHandler(e:Event):void{
- var loader_t:Loader = e.currentTarget.loader as Loader ;
- var asset_:Bitmap = new Bitmap() ;
- var bitmapdata_t:BitmapData = new BitmapData(loader_t.width,loader_t.height);
- bitmapdata_t.draw(loader_t);
- asset_.bitmapData = bitmapdata_t ;
- /*創建當前需要編輯的用戶圖像*/
- _currentEditImg = new Image() ;
- _currentEditImg.source = asset_ ;
- _currentEditImg.width = bitmapdata_t.width ;
- _currentEditImg.height= bitmapdata_t.height ;
- _currentEditImg.x = (this.width-menuID.width-loader_t.width)/2 ;
- _currentEditImg.y = (this.height-loader_t.height)/2 ;
- this.addChild(_currentEditImg) ;
- /*創建紅色矩形裁剪框*/
- _rectBorder = new Sprite() ;
- _rectBorder.x = this._x ;
- _rectBorder.y = this._y ;
- /*設置裁剪區域邊框樣式*/
- _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
- _rectBorder.graphics.beginFill(0x330000,0);
- _rectBorder.graphics.drawRect(0,0,this._w,this._h);
- _rectBorder.graphics.endFill();
- this._currentEditImg.addChild(_rectBorder);
- /*創建光標對象*/
- _cursor = new Image();
- /*一定要用這種方式給光標傳值,否則會初相亂七八糟的效果。真他媽的鬱悶,搞了好幾天才發現,估計是flex的bug*/
- _cursor.source = flag ;
- _cursor.width = 30 ;
- _cursor.height = 30 ;
- _cursor.visible = false ;
- this._currentEditImg.addChild(_cursor);
- this._currentEditImg.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
- this._currentEditImg.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler) ;
- this._currentEditImg.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
- this._currentEditImg.addEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler) ;
- this._currentEditImg.doubleClickEnabled = true ;
- }
- /**
- * 雙擊應用修改
- */
- private function doubleClickHandler(e:MouseEvent):void{
- this.applicationHandler() ;
- }
- /**
- * 鼠標按下
- */
- private function mouseDownHandler(e:MouseEvent):void{
- this._tempObj.x = _rectBorder.x ;
- this._tempObj.y = _rectBorder.y ;
- this._tempObj.width = this._w ;
- this._tempObj.height = this._h ;
- this.mouseState = 1 ;
- }
- /**
- * 鼠標彈起
- */
- private function mouseUpHandler(e:MouseEvent):void{
- this.mouseState = 0 ;
- this._rectBorder.stopDrag() ;
- }
- /**
- * 鼠標移動及拖動處理
- */
- private function mouseMoveHandler(e:MouseEvent):void{
- _mx = mouseX - this._currentEditImg.x ;
- _my = mouseY - this._currentEditImg.y ;
- _cursor.x = _mx-_cursor.width/2 ;
- _cursor.y = _my - _cursor.height/2 ;
- this._cursor.visible = false ;
- Mouse.show();
- /*光標在左邊邊緣拖動*/
- if((_criticalFlag==1) && this.mouseState == 1){
- leftDragHandler();
- }
- /*光標在右邊邊緣拖動*/
- else if((_criticalFlag==2) && this.mouseState == 1){
- this.rightDragHandler();
- }
- /*光標在中間拖動*/
- else if((_criticalFlag==3) && this.mouseState ==1){
- var rt:Rectangle = new Rectangle();
- rt.x = rt.y = 0 ;
- rt.width = _currentEditImg.width - this._w ;
- rt.height = _currentEditImg.height - _h ;
- this._rectBorder.startDrag(false,rt) ;
- this.mouseState = 2 ;
- }
- //在左邊緣
- if(isLeftEdge()){
- this._cursor.visible = true ;
- Mouse.hide() ;
- _criticalFlag =1;
- }
- else if(isRightEdge()){
- this._cursor.visible = true ;
- Mouse.hide() ;
- _criticalFlag =2;
- }else if(isInCutRect()){
- _criticalFlag = 3;
- }
- else{
- _criticalFlag = 0 ;
- }
- e.updateAfterEvent() ;
- }
- /**
- * 在左邊緣上拖動處理
- */
- private function leftDragHandler():void{
- _rectBorder.graphics.clear();
- _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
- _rectBorder.graphics.beginFill(0xCCFF00, 0);
- /*左邊邊界*/
- _rectBorder.x = _mx < 0?0:_mx ;
- /*右邊邊界*/
- _rectBorder.x = _rectBorder.x >= (_w + _tempObj.x)?(_w + _tempObj.x):_rectBorder.x ;
- /*裁剪框寬度*/
- _w = _tempObj.width + _tempObj.x - _rectBorder.x ;
- /*裁剪框高度*/
- _h = _w / this._targeimgtscale ;
- var h2:Number = this._currentEditImg.height - this._tempObj.y ;
- /*是否撐到最大*/
- if(_h>h2){
- _rectBorder.x = this._tempObj.x + this._tempObj.width -h2*this._targeimgtscale;
- _w = h2* this._targeimgtscale ;
- this._h = h2 ;
- }
- _rectBorder.graphics.drawRect(0, 0, _w, _h );
- _rectBorder.graphics.endFill();
- }
- private function rightDragHandler():void{
- _rectBorder.graphics.clear();
- _rectBorder.graphics.lineStyle(2, 0xFF0000, 0.5, true, LineScaleMode.NONE, CapsStyle.ROUND);
- _rectBorder.graphics.beginFill(0xCCFF00, 0);
- _w= _mx > _currentEditImg.width?_currentEditImg.width:_mx ;//右邊界判斷
- _w = (_w <= this._tempObj.x)?_tempObj.x:_w ; //左邊界判斷
- _w = _w-this._tempObj.x ; //求出矩形寬度
- _h = _w/ this._targeimgtscale ; //求出高度
- _rectBorder.y = _tempObj.height + _tempObj.y - _h; //求出最大允許高度
- if (_rectBorder.y < 0) {
- _h = _tempObj.height + _tempObj.y ;//最大寬度爲矩形寬度
- _rectBorder.y = 0;
- _w = _h * this._targeimgtscale ; //求出高度
- }
- _rectBorder.graphics.drawRect(0, 0, _w,_h);
- _rectBorder.graphics.endFill();
- }
- /**
- * 拖動整個裁剪矩形
- */
- private function isStartMiddleDrag():Boolean{
- if((_criticalFlag==3) && this.mouseState ==1){
- return true ;
- }
- return false ;
- }
- /**
- * 檢查光標是否在左邊界上
- * @return boolean
- */
- private function isLeftEdge():Boolean {
- /**檢查左邊界*/
- if(_mx<0){
- return false ;
- }
- if (_mx>=(this._rectBorder.x -BORDER1) &
- _mx<=(this._rectBorder.x + BORDER1) &&
- _my>this._rectBorder.y &&
- _my<(this._rectBorder.y +_h)) {
- return true ;
- }
- return false ;
- }
- /**
- * 檢查光標是否在右邊界上
- */
- private function isRightEdge():Boolean {
- if(_mx>this._currentEditImg.width){
- return false;
- }
- if (_mx>=(this._rectBorder.x + _w - BORDER1) && _mx<=(this._rectBorder.x+_w+BORDER1) &
- _my>this._rectBorder.y&& _my<(this._rectBorder.y+_h)) {
- return true ;
- }
- return false ;
- }
- /**
- * 判斷光標標是否在裁剪區域裏面
- * @return true 如果光標在裏面
- */
- private function isInCutRect():Boolean {
- if (_mx>(this._rectBorder.x +BORDER1) &&
- _mx<(this._rectBorder.x + _w - BORDER1) &
- _my>(this._rectBorder.y + BORDER1) &&
- _my<(this._rectBorder.y + _h -BORDER1)) {
- return true ;
- }
- return false ;
- }
- private function applicationHandler():void{
- //照片編輯,保存座標參數
- _p.saveCurrentPageData(_p.currentpageNum,_p.currentphotoNum,_url,_rectBorder.x,_rectBorder.y,_w,_h);
- _p.loadphotoToStage(_targetimg,_p.currentphotoNum,false,this._url)
- this.cancleHandler()
- }
- private function cancleHandler():void{
- this._currentEditImg.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
- this._currentEditImg.removeEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
- this._currentEditImg.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
- this._currentEditImg.removeEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandler);
- PopUpManager.removePopUp(this);
- }
- public var BORDER1:Number = 10 ;
- private function mouseOverHelp():void{
- this.helphintID.visible = true ;
- this.removeChild(this.helphintID);
- this.addChildAt(this.helphintID,this.numChildren);
- }
- private function mouserOutHelp():void{
- this.helphintID.visible = false ;
- }
- ]]>
- </mx:Script>
- <mx:Canvas id="menuID" x="530" y="0" width="70" height="510" backgroundColor="#DCDCDC" borderStyle="solid" borderThickness="2">
- <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"/>
- <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"/>
- <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"/>
- </mx:Canvas>
- <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">
- <mx:Text x="8" y="2" text="紅色框內爲您需要的照片內容,光標落在紅色框內時,按住鼠標左鍵任意角度拖動可移動框的位置; 光標落在紅色框左右兩條邊線時,會變成左右方向的箭頭形狀,此時按住鼠標左鍵左右拖動可等比例改變框的大小; 移動紅色框的位置或改變紅色框的大小直到取得您滿意的照片內容後,雙擊照片區域或點擊“應用”按鈕返回製作頁面; 點擊“取消”按鈕也可返回製作頁面,且不會對照片的位置和大小做任何改變。" width="292" height="220" fontSize="12" fontWeight="normal" color="#454545"/>
- </mx:Canvas>
- </mx:Canvas>
flex 開發卡片照片編輯篇(二)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.