最近在做東西的時候要用到映像的裁剪,自然就要用到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;
}