玩过截图的人都知道截图是怎样一回事,通过截取图片一个地方就能够获取到图片某一个片段。这一个部分在flash 里面可以利用位图的复制像素来实现。通过copyPixels 或者draw 能够获取像素。
现在我们讨论一个话题:在flash里面怎样实现这种截图? 带着这个问题,尝试制作一个demo出来。功能就是获取位图。
所需要的材料:第一就是flash绘图API ,第二就是位图像素操作。利用这两个我们就能够实现出这种交互效果。
设计的图案:
制作思路:首先知道怎样绘制一个矩形,可以上网搜索一下,或者查询一些flash绘图API 是如何操作的。借助绘制这股矩形,我们就能截取到我们所需要的片段。
接下来,为程序写一个基类:Item.as
定义一些常用的方法和属性。
例如绘制矩形。获取位图,定义线条的样式等,判断矩形是否有效
这些方法都是定义在这个基础类当中。
package org.summerTree{ //选择类基本设置 import flash.display.Sprite; import flash.geom.Rectangle; import flash.events.EventDispatcher; import flash.display.Bitmap; import flash.display.BitmapData; import flash.geom.Point; public class Item extends EventDispatcher { public var LineColor:int=0x0000ff;//线条颜色 public var FillColor:int=0x0000ff;//填充的颜色 public var FillAlpha:Number=0.1; public function Item() { } //绘制矩形的基本方法 protected function DrawRect(shape:Sprite,rect:Rectangle):void { shape.graphics.clear(); shape.graphics.lineStyle(1,LineColor,1); shape.graphics.beginFill(FillColor,FillAlpha); shape.graphics.drawRect(rect.x,rect.y,rect.width,rect.height); shape.graphics.endFill(); } //判断rect是否有效 public function checkRect(rect:Rectangle):Boolean { return rect != null?true:false; } //定义样式 public function setStyle(lcolor:int,fcolor:int,aplha:Number):void { this.LineColor=lcolor; this.FillColor=fcolor; this.FillAlpha=aplha; } //获取位图 public function getImage(source:Bitmap,rect:Rectangle):Bitmap { var bitmapdata:BitmapData = source.bitmapData; var Width:Number=Math.abs(rect.width); var Height:Number=Math.abs(rect.height); var temp:Rectangle; var newImage:BitmapData; if (rect.width>0 && rect.height>0) { newImage=new BitmapData(Width,Height, false);// newImage.copyPixels(bitmapdata,rect,new Point(0,0)); } else if (rect.width<0 && rect.height>0) { newImage=new BitmapData(Width,Height, false);// temp=new Rectangle(rect.x-Width,rect.y,Width,Height); newImage.copyPixels(bitmapdata,temp,new Point(0,0)); } else if (rect.width<0 && rect.height<0) { newImage=new BitmapData(Width,Height, false);// temp=new Rectangle(rect.x-Width,rect.y-Height,Width,Height); newImage.copyPixels(bitmapdata,temp,new Point(0,0)); } els