将显示对象绘制为位图数据

辅助函数,用于创建对象的位图副本。这可用于将矢量对象,文本或复杂的嵌套 Sprite 转换为展平位图。

function makeBitmapCopy(displayObj:IBitmapDrawable, transparent:Boolean = false, bgColor:uint = 0x00000000, smooth:Boolean = true):Bitmap {

    //create an empty bitmap data that matches the width and height of the object you wish to draw
    var bmd:BitmapData = new BitmapData(displayObj.width, displayObj.height, transparent, bgColor);
            
    //draw the object to the bitmap data
    bmd.draw(displayObj, null, null, null, null, smooth);
            
    //assign that bitmap data to a bitmap object
    var bmp:Bitmap = new Bitmap(bmd, "auto", smooth);
            
    return bmp;
}

用法:

var txt:TextField = new TextField();
txt.text = "Hello There";

var bitmap:Bitmap = makeBitmapCopy(txt, true); //second param true to keep transparency
addChild(bitmap);