4 Replies Latest reply on Jun 18, 2012 3:27 PM by drkstr_1

    Screen dimensions not updating in time for bindings?

    jlehenbauer-m Level 1

      I have an app that is displaying images. It resizes them based on screen rotation, usually setting "picwidth = screen.width", where "picwidth" is an attribute that the width of each picture is bound to.

       

      So I have several functions similar to:

       

      protected function changeOrientation(e:StageOrientationEvent):void

                                    {

                                              picwidth = screen.width;

                                              picheight = screen.width*1.467;

                     }

       

      I have also tried doing this with the variable of the object itself:

       

      protected function changeOrientation(e:StageOrientationEvent):void

                                    {

                                              img.width = screen.width;

                                              img.height = screen.width*1.467;

                     }

       

      and in the emulator, these functions work properly. But on the device, "screen.width" always seems to grab the value before the orientation change, giving dimensions that are exactly opposite of what I needed.

       

      NOTE: this does not just occur with images, it happens with Group containers, TextAreas, anything whose dimensions depend on the screen dimensions.

       

      I have tried simply switching the values so it gets the values and assigns them to the opposite variables to account for the delay, and they work on my device then, but they are backwards in the emulator and I am afraid other devices will function as the emulator does, and it will be broken once again.

       

      Is there any good solution to this?

        • 1. Re: Screen dimensions not updating in time for bindings?
          jlehenbauer-m Level 1

          This problem was fixed by:

           

          change the event listener from:

           

          stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, changeView);

           

          to:

           

          stage.addEventListener(Event.RESIZE, changeView);

           

          and passing an "Event" instead of a "stageOrientationEvent".

           

          worked in every instance I needed. It's kindof a shame that the only thing that StageOrientationEvent.ORIENTATION_CHANGE is designed to do can be outperformed by a more general function..

          • 2. Re: Screen dimensions not updating in time for bindings?
            drkstr_1 Level 4

            You are correct for listening to a resize event, to perform logic that depends on the size of the container. ORIENTATION_CHANGE is not intended for this purpose. Instead, it is intended to be used when the orientation changes. For example, if you wanted to implement separate views for landscape and portrait. An orientation change is not the same as a "resize," and even then, you should rely on the actual resize event to perform size related logic.

            • 3. Re: Screen dimensions not updating in time for bindings?
              jlehenbauer-m Level 1

              I was trying to do exactly this. I had two views, one that would be displayed while the device was in "landscape" mode and the other in "portrait". The orientation change event would fire, but the operations that were attatched to it would use the values of the screen dimensions before the orientation change. But you're saying that this event occurs before the dimensions actually register a change to the system?

              • 4. Re: Screen dimensions not updating in time for bindings?
                drkstr_1 Level 4

                jlehenbauer-m wrote:

                 

                I was trying to do exactly this. I had two views, one that would be displayed while the device was in "landscape" mode and the other in "portrait". The orientation change event would fire, but the operations that were attatched to it would use the values of the screen dimensions before the orientation change. But you're saying that this event occurs before the dimensions actually register a change to the system?

                 

                Nope. I am saying that listening for anything other than a ResizeEvent.RESIZE is unreliable, when you need to know when a UIComponent has been resized. Element sizing is not always instantaneous.The display list needs to go through an invalidate/measure/update cycle before the final size and position of an element can be known. Listening for a ResizeEvent.RESIZE will ensure you are in the right stage of the cycle, when performing logic that depends on that size. I'm actually not sure when the StageOrientationEvent.ORIENTATION_CHANGE event is dispatched, but I assume it's what triggers the actual update cycle, so I would also assume the new size would be unknown at that time.