Flash AS3:图像裁剪

时间:2008-09-03 02:06:18  来源:闪吧  作者:passi0n  【背景色 杏仁黄 秋叶褐 胭脂红 芥末绿 天蓝 雪青 灰 银河白

最近在做东西的时候要用到图像的裁剪,自然就要用到bitmapData了。很早以前做过基于透明像素的橡皮檫,这个图像裁剪的原理和橡皮檫的原理一样。也许很久没用又出于陌生了,所以决定好好在做一次,又加深些印象。哈哈...

(双击上面那个心生成图像)

以下是图像裁剪核心的代码:

function cutLayer_doubleClick_handler(me:MouseEvent)

cutBtn.mouseEnabled=true;
isCut=false 
itf.actionExit(); //缩放编辑对象退出动作

//temp_bmd主要用于copyPixels ()时候alphaBitmapData的获得,简单理解就是遮照层不规则图象获得
var temp_bmd:BitmapData=new BitmapData(org_sprite.width,org_sprite.height,true,0)
//compare_bmd主要是从源bitmapData在遮照层(不规则图象)上复制的象素图象
var compare_bmd:BitmapData=new BitmapData(org_sprite.width,org_sprite.height,true,0)

//画遮照层对图象的位移,缩放,旋转设置
var temp_matrix:Matrix=new Matrix();

temp_matrix.scale(cut_layer.scaleX,cut_layer.scaleY); 
temp_matrix.rotate(cut_layer.rotation/180*Math.PI);
temp_matrix.translate(cut_layer.x,cut_layer.y); 

//因为遮照层采用了一个遮照Mc和一个透明图象,如果copyPixels时不将alphaBitmapData的透明度为最大.那么copy的图象和alphaBitmapData的透明度是一样的
var temp_ctf:ColorTransform=new ColorTransform()
temp_ctf.alphaOffset=255

//画不规则遮照层
temp_bmd.draw(cut_layer,temp_matrix,temp_ctf);

//复制象素
compare_bmd.copyPixels(org_bmd,
org_sprite.getBounds(this),
new Point(0,0),
temp_bmd,
new Point(0,0),
false);

//显现复制图象
compare_bm.bitmapData=compare_bmd;
}


文章评论

共有 0位网翼网友发表了评论 查看完整内容