用户名: 密码: 验证码: QQ--程序群:31736530 动画群:38836599
闪无忧
 
首 页 业界新闻 业界杂谈 Flash教程 Flash源码 Flash图书 Flash酷站 Flex & AIR 供求信息
   本栏目通告:   有意向写收费精品教程的朋友,请联系本站合作
当前位置 :首页>flash教程>Flash as3.0教程>列表

AS3图像处理之剪裁、动态选取

[来源:圣叹 | 作者:圣叹 | 时间:2008-06-09 | 点击:  | 收藏本文  【 】]



和师傅写C#写的思维混乱,方法变量几乎第一反应就是大写,习惯都被改写了,.NET在代码管理上的确比Flex强悍的多。
OK,进入主题,毕设还没结束就先回公司报道了,哈哈,接下来事情不少。这个是一个项目开始要用到的,一大堆的Rectangle运算处理,头痛了一整天,其中的SelectRectanle用于选取框的生成,可拖拽,缩放,返回一个给定可视元素的被选区域(Rectangle), CutImage用于剪裁图片。

查看源代码:http://www.moorwind.com/as3app/imageprocess/srcview/index.html

代码中注意,因为要对图片进行操作,所以使用了URLStream()加载待处理图像。
  1. //选取框   
  2. package com.moorwind.imageprocesser.utils   
  3. {   
  4.   import com.moorwind.imageprocesser.ui.ScaleButton;   
  5.      
  6.   import flash.display.Sprite;   
  7.   import flash.events.Event;   
  8.   import flash.events.MouseEvent;   
  9.   import flash.geom.Rectangle;   
  10.   import flash.net.URLLoader;   
  11.      
  12.   public class SelectRectangle extends URLLoader   
  13.   {   
  14.     private var target:Sprite   
  15.     private var onlayer:Sprite;   
  16.     private var startX:Number;   
  17.     private var startY:Number;   
  18.     private var dragX:Number;   
  19.     private var dragY:Number;   
  20.        
  21.     private var drawRect:Rectangle;   
  22.     private var scaleButton:ScaleButton;   
  23.        
  24.     public static const SELECTED:String = "selected";   
  25.        
  26.     public var lineColor:int = 0x0099FF;   
  27.     public var fillColor:int = 0x0099FF;   
  28.     public var fillAlpha:Number = 0.1;   
  29.        
  30.     public var isSelectRectangleFinished:Boolean = false;   
  31.     public var isScaleable:Boolean = false;   
  32.        
  33.     public function SelectRectangle(target:Sprite, onlayer:Sprite)   
  34.     {   
  35.       this.target = target;   
  36.       this.onlayer = onlayer;   
  37.       this.onlayer.buttonMode = true;         
  38.     }   
  39.        
  40.        
  41.     //get selected rectangle   
  42.     public function get selectRectangle():Rectangle   
  43.     {         
  44.       return this.isSelectRectangleFinished ? this.drawRect : null;   
  45.     }   
  46.        
  47.     // initial rectangle   
  48.     public function InitSelectRectangle():void  
  49.     {   
  50.       if(this.isSelectRectangleFinished)   
  51.         return;   
  52.          
  53.       this.target.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);   
  54.     }   
  55.                
  56.     private function StartDrawRect(e:MouseEvent):void  
  57.     {   
  58.       this.startX = this.target.mouseX;   
  59.       this.startY = this.target.mouseY;     
  60.       this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);   
  61.       this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);   
  62.     }   
  63.     private function DrawingRect(e:MouseEvent):void  
  64.     {   
  65.       this.drawRect = new Rectangle(this.startX,this.startY,this.target.mouseX - this.startX,this.target.mouseY - this.startY);   
  66.       this.onlayer.graphics.clear();   
  67.       this.onlayer.graphics.lineStyle(1,this.lineColor,1);   
  68.       this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);   
  69.       this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);   
  70.       this.onlayer.graphics.endFill();    
  71.                
  72.       e.updateAfterEvent();   
  73.     }   
  74.     private function EndDrawRect(e:MouseEvent):void  
  75.     {   
  76.       this.isSelectRectangleFinished = true;   
  77.       this.target.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDrawRect);   
  78.       this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DrawingRect);   
  79.       this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDrawRect);   
  80.          
  81.       this.scaleButton = new ScaleButton();   
  82.       this.onlayer.addChild(this.scaleButton);   
  83.       this.scaleButton.x = this.drawRect.x+this.drawRect.width;   
  84.       this.scaleButton.y = this.drawRect.y+this.drawRect.height;   
  85.          
  86.       this.dispatchEvent(new Event(SelectRectangle.SELECTED));   
  87.          
  88.       this.StartScaleButton();   
  89.       this.InitDragEvent();   
  90.     }   
  91.        
  92.     // scale rectangle   
  93.     public function StartScaleButton():void  
  94.     {   
  95.       this.scaleButton.addEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);         
  96.     }   
  97.        
  98.     private function StartScaleRect(e:MouseEvent):void  
  99.     {   
  100.       this.isScaleable = true;           
  101.       this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);   
  102.       this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);         
  103.     }   
  104.     private function ScalingRect(e:MouseEvent):void  
  105.     {         
  106.       this.drawRect = new Rectangle(this.drawRect.x,this.drawRect.y,this.target.mouseX - this.drawRect.x,this.target.mouseY - this.drawRect.y);   
  107.       this.onlayer.graphics.clear();   
  108.       this.onlayer.graphics.lineStyle(1,this.lineColor,1);   
  109.       this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);   
  110.       this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);   
  111.       this.onlayer.graphics.endFill();    
  112.                    
  113.       this.scaleButton.x = this.drawRect.x+this.drawRect.width;   
  114.       this.scaleButton.y = this.drawRect.y+this.drawRect.height;   
  115.          
  116.       e.updateAfterEvent();   
  117.     }       
  118.     private function EndScaleRect(e:MouseEvent):void  
  119.     {   
  120.       this.isScaleable = false;         
  121.       this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.ScalingRect);   
  122.       this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndScaleRect);       
  123.          
  124.       this.dispatchEvent(new Event(SelectRectangle.SELECTED));   
  125.     }   
  126.        
  127.     // drag rectangle   
  128.     public function InitDragEvent():void  
  129.     {   
  130.       this.onlayer.addEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);         
  131.     }   
  132.        
  133.     private function StartDragRect(e:MouseEvent):void  
  134.     {   
  135.       if(this.isScaleable)   
  136.         return;   
  137.            
  138.       this.dragX = this.target.mouseX - this.drawRect.x;   
  139.       this.dragY = this.target.mouseY - this.drawRect.y;   
  140.       this.target.addEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);   
  141.       this.target.addEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);   
  142.     }   
  143.     private function DragingRect(e:MouseEvent):void  
  144.     {   
  145.       this.drawRect = new Rectangle(this.target.mouseX - this.dragX,this.target.mouseY - this.dragY,this.drawRect.width, this.drawRect.height);   
  146.       this.onlayer.graphics.clear();   
  147.       this.onlayer.graphics.lineStyle(1,this.lineColor,1);   
  148.       this.onlayer.graphics.beginFill(this.fillColor,this.fillAlpha);   
  149.       this.onlayer.graphics.drawRect(this.drawRect.x,this.drawRect.y,this.drawRect.width,this.drawRect.height);   
  150.       this.onlayer.graphics.endFill();    
  151.                    
  152.       this.scaleButton.x = this.drawRect.x+this.drawRect.width;   
  153.       this.scaleButton.y = this.drawRect.y+this.drawRect.height;   
  154.          
  155.       e.updateAfterEvent();         
  156.     }   
  157.     private function EndDragEvent(e:MouseEvent):void  
  158.     {   
  159.       this.target.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragingRect);   
  160.       this.target.removeEventListener(MouseEvent.MOUSE_UP, this.EndDragEvent);   
  161.       this.dispatchEvent(new Event(SelectRectangle.SELECTED));   
  162.     }   
  163.        
  164.     // cancel selected box   
  165.     public function Cancel():void  
  166.     {   
  167.       this.onlayer.graphics.clear();   
  168.                
  169.       this.onlayer.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartDragRect);     
  170.       this.scaleButton.removeEventListener(MouseEvent.MOUSE_DOWN, this.StartScaleRect);       
  171.          
  172.       this.onlayer.removeChild(this.scaleButton);   
  173.       this.isSelectRectangleFinished = false;   
  174.     }   
  175.        
  176.   
  177.   }   
  178. }   


  1. //剪裁图像   
  2. package com.moorwind.imageprocesser.edit   
  3. {   
  4.   import flash.display.Bitmap;   
  5.   import flash.display.BitmapData;   
  6.   import flash.geom.Point;   
  7.   import flash.geom.Rectangle;   
  8.      
  9.   public class CutImage   
  10.   {   
  11.     public static function Cut(data:Bitmap, rect:Rectangle):Bitmap   
  12.     {   
  13.       var bitmapdata:BitmapData = data.bitmapData;   
  14.       var retdata:BitmapData = new BitmapData(rect.width, rect.height, false);   
  15.       retdata.copyPixels(bitmapdata,rect,new Point(0,0));   
  16.       return new Bitmap(retdata);   
  17.     }   
  18.   
  19.   }   
  20. }  
文章如果有错误或者缺少文件,请发邮件提交给我们
上一篇:as3中一些不太常见的代码写法
下一篇:ActionScript 3设计模式类图(一)
Tags:    
>>> 最新评论:(共有 0 位网友发表了评论)      查看所有评论
  发表评论
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
·本站发布内容均为客观表达作者观点,不代表闪无忧立场,请勿攻击和漫骂
·用户发表意见仅代表其个人意见,并且承担一切因发表内容引起的纠纷和责任
·本站管理人员有权在不通知用户的情况下删除不符合规定的评论信息或留做证据
·请客观的评价您所看到的资讯,提倡就事论事,杜绝漫骂和人身攻击等不文明行为
  教程分类
  基础操作   动画特效
  应用开发   组件学习
  As程序   动画教程
  Flash cs3   AS 3.0
  FCS/FMS教程   Loading教程
  Flash与Web   Flash教程连载
  相关文章
·ActionScript 3.0垃圾回收
·Flash上传客户端
·AS3编码转换函数
·flash as3的binding-ObjectProxy
·as3的ComboBox组件应用小问题
·小球的弹性碰撞
·小球的弹性碰撞
·小球的弹性碰撞
·ActionScript 3设计模式类图(二)
·ActionScript 3设计模式类图(一)
  热门文章
·Flash进度条的制作详细讲解(组图)
·flash幻灯片网页效果
·Flash打造简单的飘雪动画视觉特效
·Flash旋转拖尾文字效果的制作教程
·flash水影效果字
·全Flash动画网站实现的基础教学
·超酷flash光晕移动效果
·flash春雷闪电效果
·Flex 3 AdvancedDataGrid的使用(第二
·Flash制作大雪纷飞效果动画
·即拷即用的loading代码
关于我们 - 免责声明 - 网站地图 - 商务服务 - 联系我们 - RSS地图
©CopyRight 2006-2008, 5UFlash.COM, Inc. All Rights Reserved
鲁ICP备06034971号