监听 Render 事件是好是坏?
我想要实现的是扩展 Flex 中的组件并添加一个始终在其旁边显示的图像。 (图像无法添加到控件内部(addElement/addChild))
我已经计算了控件的坐标,并设法将图像定位到始终位于其旁边的位置,而在 Creation_Complete
事件中没有任何问题。但我注意到,当调整屏幕大小并且更改控件的位置时,图像仍然保持在相同的位置。
因此,我监听了 Render 事件并进行了坐标计算,将图像放置在控件旁边。现在这工作得很好。
但我遇到的问题是监听 Render 事件进行坐标计算是否对性能不利?是否还有其他不太频繁触发的事件来完成我的工作?
希望它是清楚的,并提前非常感谢:)
What I am trying to achieve is to extending a component in Flex and add an image to be shown ALWAYS next to it. (The image cannot be added inside (addElement/addChild) the control)
I have calculated the coordinates of the control and managed to position to the image always next to it without any issues in the Creation_Complete
event. But what I noticed was, when the screen is resized and the position of the control is changed, the image still stays in the same location.
So I listened to the Render
event and did the coordinate calculation to place the image right next to the control. Now this works perfectly.
But the issue I have is whether listening to the Render
event to do the coordinate calculation is bad for performance? Is there a another event that is less frequently fired, to do my job?
Hope it is clear and thanx a lot in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在谈论调整屏幕大小,那么您还可以监听调整大小事件(在舞台外),而不是渲染事件(在图像上)。
仅当调整舞台大小时才会触发。
You are talking about resizing of the screen, then you could also listen to the resize event (off the stage), instead of the render event (on the image).
This should only fire when there is a resizing of the stage.
如果您要扩展组件,您可以尝试使用高级 Flex 组件生命周期,重写 updateDisplayList() 方法将组件放置在您认为合适的位置,同时考虑屏幕尺寸以及元素是否可见等。当然,您可以可能希望使用一个简单的函数在内部利用调整大小事件,该函数仅调用 invalidateDisplayList(),确保不会进行必要的重新定位调用。这是一个方法/updateDisplayList()/,在渲染阶段仅调用一次。此外,为了将新元素添加到扩展组件中,重写 createChildren() 来创建和添加图像也是合理的。在此阶段不需要任何其他操作,因为如果添加/删除子项,将自动调用 updateDisplayList()。
组件生命周期说明 - 这对我有很大帮助:")
If you are extending a component you may try to use the superior Flex components lifecycle, overriding updateDisplayList() method to position the component where you do see fit, taking into consideration the screen size and will the element be visible, etc. Of course you may want to internally tap into resize event with a simple function that will only call invalidateDisplayList(), making sure that no neccessary calls will for reposition will be made. This is a method /the updateDisplayList()/ that is called only once during render stage. Also for adding the new element into your extended component it would be reasonable to override createChildren() to create and add the image. During that phase nothing else is required, since updateDisplayList() will be automaticly called if child is added/removed.
Component Lifecycle Explanation - This one was of a great help for me :")
作为解决方法,我最终听了一次 Render,但是如果您在 Render 处理程序中设置断点,您可能会注意到它被调用了 1000 次:我会说 BAD。
我建议将调整大小事件添加到应用程序标记
resize="application_resize(event);
I ended up listening to Render once as a workaround, but if you set a breakpoint in the Render handler, you will probably noitce that it is called 1000's of times: I would say BAD.
I would suggest a resize event added to the Application tag
resize="application_resize(event);