Last active
October 11, 2015 03:57
-
-
Save lynxerzhang/3798839 to your computer and use it in GitHub Desktop.
FlashIDE中获取导入图片的类型(shape or bitmap)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//将一张图片资源导入至Flash IDE中, 将其转化为MovieClip类型, trace它内部的这张图片得到的类型为Shape, 如果需要得到原始的Bitmap类型,需将库中对应图片资源添加链接名, 以作为BitmapData类型的子类, 同时得到的该Bitmap对象的大小为原始尺寸, (通过transform.matrix可以获得),如需获取在该mc中的实际尺寸(可以使用transform.concatenatedMatrix获取) | |
/**http://stackoverflow.com/questions/4408496/scale9grid-and-bitmaps-do-they-work-together-or-not**/ | |
/**http://www.ovidiudiac.ro/blog/2009/05/scale9grid-work-and-fail/**/ | |
//Graphics类有了beginBitmapFill方法, 实现Bitmap的9slice就方便了很多 | |
function convertBitmaptoScale9(mc:MovieClip, scale9Grids:Rectangle):void{ | |
var b:Bitmap = mc.removeChildAt(0) as Bitmap; //获取到的是Bitmap而不是Shape,就是因为在库中取了链接名 | |
var topDown:Array = [scale9Grids.top, scale9Grids.bottom, b.height]; | |
var leftRight:Array = [scale9Grids.left, scale9Grids.right, b.width]; | |
var g:Graphics = mc.graphics; | |
var bd:BitmapData = b.bitmapData.clone(); | |
var topDownStepper:int = 0; | |
var leftRightStepper:int = 0; | |
for(var i:int = 0; i < 3; i ++){ | |
for(var j:int = 0; j < 3; j ++){ | |
g.beginBitmapFill(bd, null, true, true); | |
g.drawRect(leftRightStepper, topDownStepper, | |
leftRight[i] - leftRightStepper, topDown[j] - topDownStepper); | |
g.endFill(); | |
topDownStepper = topDown[j]; | |
} | |
leftRightStepper = leftRight[i]; | |
topDownStepper = 0; | |
} | |
mc.scale9Grid = scale9Grids; | |
b.bitmapData.dispose(); | |
b.bitmapData = null; | |
} | |
//example | |
var r:Rectangle = new Rectangle(23, 21, 44, 47);//9slice 尺寸 | |
convertBitmaptoScale9(_mc, r);//清除原始Bitmap信息,转而在该mc的graphics中绘制 | |
//scale | |
_mc.width = 500; | |
_mc.height = 400; | |
_mc.x = 0; | |
_mc.y = 0; | |
trace(_mc.numChildren); // 0 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment