Flex:如何调整 DisplayObject 大小以适应 Flex 应用程序中的面板

发布于 2024-08-29 19:38:02 字数 1427 浏览 10 评论 0原文

我试图将一些从 Sprite 继承的 actionscript 类附加到我的 Flex 应用程序,方法是将其附加到 UIComponent,然后将 UIComponent 添加到面板。但是,我的 Sprite 类的大小似乎比面板大。因此,我尝试使用 DisplayObject.scale 属性调整它的大小,但我需要知道容器的大小。因为我试图使我的代码可重用,所以我认为我应该让我的 Sprite 类通过其父属性获取它来处理调整大小(比方说,我可以通过设置

this.scaleX = this.parent.Width/this.宽度或类似的东西)

但是,我的 Sprite 类的父级(即 UIComponent)的宽度/高度为 0。 所以,我的问题是: 1)是否有更好的方法来调整 DisplayObject 类的大小以在附加到 Flex 对象时适合其容器? 2)如果你知道,为什么我的 UIComponent 的大小保持为 0 而我的 DisplayObject 仍然以其完整大小显示?

这是我的代码,使用 Flash Builder 4:

private var loader:Loader = new Loader();
private function testLoader():void {
  var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png");
  loader.load(urlRequest);
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete);
}

private function onLoaderLoadComplete(e:Event):void {
  loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete);
  var ui : UIComponent = new UIComponent();
  ui.addChild(loader);
  panelTest.addElement(ui);
  trace("ui.width = " + ui.width);
  trace("ui.height = " + ui.height);
  trace("loader.width = " + loader.width);
  trace("loader.height = " + loader.height);
  trace("panelTest.width = " + panelTest.width);
  trace("panelTest.height = " + panelTest.height);
}



这是运行时的结果:

ui.width = 0
ui.height = 0
loader.width = 571
loader.height = 411
panelTest.width = 480
panelTest.height = 320

I am trying to attach some of my actionscript class, inherited from Sprite, to my Flex application by attaching it to a UIComponent, then add UIComponent to a panel. However, the size of my Sprite class appears to be larger than the panel. So, I try to resize it using DisplayObject.scale property, but I need to know the size of my container. Since I try to make my code reusable, I figure that I should let my Sprite class handle the resize by getting it via its parent property (let's say, I can resize it by set

this.scaleX = this.parent.Width/this.width or something like this)

However, my Sprite class's parent, which is the UIComponent, has width/height of 0.
So, my question is:
1) Is there any better way to resize DisplayObject class to fit its container when attached to Flex object?
2) If you know, why my UIComponent's size remain 0 and my DisplayObject still appears at its full size?

Here's my code, using Flash Builder 4:

private var loader:Loader = new Loader();
private function testLoader():void {
  var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png");
  loader.load(urlRequest);
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete);
}

private function onLoaderLoadComplete(e:Event):void {
  loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete);
  var ui : UIComponent = new UIComponent();
  ui.addChild(loader);
  panelTest.addElement(ui);
  trace("ui.width = " + ui.width);
  trace("ui.height = " + ui.height);
  trace("loader.width = " + loader.width);
  trace("loader.height = " + loader.height);
  trace("panelTest.width = " + panelTest.width);
  trace("panelTest.height = " + panelTest.height);
}



And this is the result when run:

ui.width = 0
ui.height = 0
loader.width = 571
loader.height = 411
panelTest.width = 480
panelTest.height = 320

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是在用心讲痛 2024-09-05 19:38:02
  1. 我认为这是相反的:父级应该在渲染时设置子元素的大小。您可以重写 updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) 方法来设置加载器子级的大小。请参阅ActionScript 中的高级可视化组件
  2. 我猜你的ui的大小并不是真正的0。大小会在布局和渲染过程之后更新。因此,在将组件添加到舞台后,您必须等待布局完成。
  1. I think it's the other way around: the parent should set the child element size while rendering. You can override the updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) method to set the size of your loader child. See Advanced Visual Components in ActionScript.
  2. I guess that the size of your ui is not really 0. The size is updated later after the layout and rendering process. So you have to wait for the layout to complete after adding a component to stage.
箜明 2024-09-05 19:38:02

关于剩下的 0,不幸的是,这是一个已知的(grrr)问题: http: //www.mail-archive.com/[email protected]/msg39998.html

您可以尝试在黑客方式(使用 mx_internal 命名空间): 获取动态创建的自定义 uicomponent 的宽度

将精灵直接添加到面板中也可能会有所帮助。这可以通过将加载程序添加到面板的 rawChildren 来完成: http://www .actionscript.org/forums/showthread.php3?p=728034

尽情享受。

About the remaining 0, unfortunately is a known (grrr) problem: http://www.mail-archive.com/[email protected]/msg39998.html

You can try to access it in a hacked way (using the mx_internal namespace): get width of dynamically created custom uicomponent

Adding the sprite directly to the panel may also be helpful. This can be done by adding the loader to the panel's rawChildren: http://www.actionscript.org/forums/showthread.php3?p=728034

Enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文