Last active
October 10, 2015 06:47
-
-
Save lynxerzhang/3649954 to your computer and use it in GitHub Desktop.
get the actual size of the DisplayObject
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
/** | |
* 获取指定显示对象的实际长宽(不论是否在舞台或者是否指定scrollRect属性) | |
* @param dis | |
*/ | |
function getActualSize(dis:DisplayObject):Rectangle{ | |
// solution one | |
// var rect:Rectangle = dis.transform.pixelBounds.clone(); | |
// if(!dis.stage){ | |
// //maybe a bug issue?? this version could work, | |
// //but why divide by five? maybe the "twips"??? | |
// rect.width /= 5; | |
// rect.height /= 5; | |
// } | |
// return rect; | |
//solution two | |
//inspiration from [email protected] and senocular.com | |
var currentTransform:Transform = dis.transform; | |
var currentMatrix:Matrix = currentTransform.matrix; | |
var globalMatrix:Matrix = currentTransform.concatenatedMatrix; | |
globalMatrix.invert(); | |
globalMatrix.concat(currentMatrix); | |
dis.transform.matrix = globalMatrix; | |
var rect:Rectangle = dis.transform.pixelBounds; | |
dis.transform.matrix = currentMatrix; //reset the position, scale and skew value | |
return rect; | |
} | |
//@see http://www.moock.org/blog/archives/000292.html | |
//这里moock提出的是通过draw进行获取可视区域的大小 | |
//@see http://karoshiethos.com/2008/10/22/as3-scrollrect-vs-height-getbounds/ | |
//这里的回复中有一个办法,提出者认为BitmapData的draw方法可以立即触发"重绘" | |
//TODO(如果设置的是mask, 则无法仅获取可视区域大小, 可以考虑使用moock的方法, 如果设置了cacheAsBitmap, 获取的区域是整块的 | |
//mask区域和原始区域的合并大小),也就等于是mask区域的大小, 不管是使用mask还是scrollRect,均可以在设置后将cacheAsBitmap设 | |
//置为true, 并打开重绘, 可以发现设置或不设置cacheAsBitmap的重绘区域差异 | |
function getActualSize(dis:DisplayObject):Rectangle{ | |
var orect:Rectangle = dis.scrollRect; | |
dis.scrollRect = null; | |
__redraw(dis); | |
dis.scrollRect = orect; | |
__redraw(dis); | |
return new Rectangle(0, 0, dis.width, dis.height); | |
} | |
function __redraw(d:DisplayObject):void{ | |
var b:BitmapData = new BitmapData(1, 1); | |
b.draw(d); | |
b.dispose(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment