1
votes

I have such a problem: I'm writing application for Android using Adobe AIR 2.6 and Flash Builder 4.5. I need to scale my resources depending on mobile device resolution. For this purpose I need to know device resolution and DPI. I'm using such a code to get it:

PlatformUtil.init(mainView.stage.stageWidth, mainView.stage.stageHeight, 
                Capabilities.screenDPI, mainView);

When I run this code on device - all OK! All resources scaled properly (on Nexus One). But when I run it on my desctop computer on flash builder simulator, and choose from devices Google Nexus One - it must have resolution 800*480, but in code I get actual size 500*375. When I'm using Capabilities class, it returns me 1024*768 (my desctop resolution). So, whats wrong with it? Why it returns me wrong device resolution? How can I solve this problem? Thanx for help.

2
I checked: air simulator returns resolution 500*375 for ALL Android devices, but each of them has different real resolution.((yozhik

2 Answers

6
votes

I solved this issue. Simulator returns valid simulator screen resolution in handler on Event.RESIZE, this can be done like this:

public class Main extends Sprite
    {       
        public function Main()
        {
            super();

            //register to add to stage
            this.stage.addEventListener(Event.RESIZE, onResize);
            this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);

            // support autoOrients
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
        }

        private function onAddedToStage(event:Event):void
        {
            this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onResize(event:Event):void
        {
            this.stage.removeEventListener(Event.RESIZE, onResize);

            //width must be bigger then height, because we in landscape mode
            var w:int = Math.max(this.stage.stageWidth, this.stage.stageHeight);
            var h:int = Math.min(this.stage.stageWidth, this.stage.stageHeight);

            //draw black background
            with( graphics ) 
            {
                beginFill(0x0)
                drawRect(0,0,w,h);
            }

            init();
        }
}

Hope it will help someone like me.

3
votes

The reason you are always seeing 500x375 as your width and height is because those are the default values of the mxmlc compiler (specifically the -default-size option). See here for details :

http://livedocs.adobe.com/flex/3/html/help.html?content=compilers_13.html

Unless you know the actual dimension of the mobile device ahead of time (which is unlikely), you have to wait for Event.RESIZE to fire before fetching stage.stageHeight. You should listen to Event.RESIZE on the stage object.