您的位置:首页 > 其它

分享一个Image类,里面有一图片切割,clone,等一些方法

2010-01-17 21:17 459 查看
一个as3的Image类,里面加了个clone,图片切割,放大,缩小等功能!
]package chessGame.vo
{
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.display.BitmapData;
import flash.display.IBitmapDrawable;

public class Image extends Sprite
{
private var _url:*;
private var _loader:Loader;
private var _id:int;
private var _canShow:Boolean=false;
private var _isLoaded:Boolean=true;
private var _s:ChessGame;
private var _x:int;
private var _y:int;
public var _bitmap:Bitmap=null;

public function Image(usrs:*=null)
{
_url=usrs;
if(_url is String)
{
init();
}else
{
if(usrs!=null)
{
_bitmap=new Bitmap(usrs.bitmapData);
this.addChild(_bitmap);
}

}
}
private function init():void
{
if(_url!=null){
_loader = new Loader();
var request:URLRequest = new URLRequest(_url);
_loader.load(request);
configureListeners(_loader.contentLoaderInfo);
}
}

private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, completeHandler);   //加载完成
dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function removeListeners(dispatcher:IEventDispatcher):void
{
dispatcher.removeEventListener(Event.COMPLETE, completeHandler);   //加载完成
dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
}
private function completeHandler(event:Event):void {
_bitmap = Bitmap(event.target.content);
addChild(_bitmap);
_canShow=true;
trace(this._url+"加载完成! ");
if(_loader!=null)
removeListeners(_loader.contentLoaderInfo);
ChessGame.instance.upSorceNum();
}

private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}
public function get id():int{
return _id;
}
public function set id(i:int):void
{
this._id=i;
}
public function get url():*
{
return _url;
}

public function unLoad():void
{
if(_bitmap!=null)
{
if(this.contains(_bitmap))
this.removeChild(_bitmap);
_bitmap.bitmapData.dispose();
_bitmap=null;
}

}
public function get bitmap():Bitmap
{
return _bitmap;
}
//克隆副本
public function clone():Image
{
var img:Image;
if(_bitmap!=null)
{
img=new Image();
var b:Bitmap=new Bitmap(_bitmap.bitmapData.clone());
img.addChild(b);
img._bitmap=b;
img._url=this._url;
}else{
img=new Image(this._url);
}
return img;

}

public function smallClone(x:int=0,y:int=0,w:int=14,h:int=28,px:int=0,py:int=0):Image
{
var img:Image;
if(_bitmap!=null)
{
img=new Image();
var b:Bitmap=new Bitmap();
b.bitmapData=new BitmapData(w,h);
var rect:Rectangle = new Rectangle(x, y, w, h);
var pt:Point = new Point(px, py);
b.bitmapData.copyPixels(_bitmap.bitmapData, rect, pt);
img._bitmap=b;
img._url=this._url;
img.addChild(b);
}else{
img=new Image(this._url);
}
return img;
}
public static function getResizeBD(source : *,w : int,h : int) : BitmapData {
if(!source is IBitmapDrawable)return null;
if(source.width == w && source.height == h)return source;
var a : Number = w / source.width;
var d : Number = h / source.height;
var mtx : Matrix = new Matrix(a, 0, 0, d, 0, 0);
var target : BitmapData = new BitmapData(w, h, true, 0);
target.draw(source, mtx, null, null, null, true);
return target;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐