2011-10-11 128 views
0

我试图让图像尺寸居中动态精灵对象,但我在高度和widh属性0。有人可以帮助我获得图像尺寸吗?我试图通过使用图像控制的各种属性,但我得到的尺寸。请参见下面的代码和内联值评论flex 4图像尺寸

MXML代码:

<mx:Image id = "imgActive" visible="false" /> 

事件处理程序:

private function addImageHandler(e:MyCustomeEve){ 
      imgActive.source = e.imageUrl; 
      imgActive.visible = true; 

      GAME_STATUS = "ITEM_SELECTED"; 
      trace(imgActive.width, 
       imgActive.height, // 0 
       imgActive.contentHeight, // 0 
       imgActive.contentWidth, // NaN 
       imgActive.content.width, // error because imgActive.content is null 
       imgActive.content.height // error because imgActive.contentis null); 
      imgActive.cacheAsBitmap = true; 
} 
+0

什么是你正在收听的事件? – Kodiak

回答

2

当您设置图片的来源,你会不得不等待它加载内容。添加一个事件监听器,如imgActive.addEventListener(Event.COMPLETE, onImgActiveComplete);

加载图像时,将调用onImgActiveComplete处理函数,然后可以分别使用imgActive.content.width和imgAcive.content.height获取图像的尺寸。

希望这有助于

大火

+0

感谢Blaze的想法。这导致了另一个问题。你可以请检查http://stackoverflow.com/questions/7724739/flex-4-wait-for-load-complete-without-freezing –

+0

我只是看着这个:D –