AIR - Resizing Native Window Proportionately

2020-07-23 03:39发布

问题:

my application launches with the stage size of 1000 x 500, 2:1 aspect ratio. the native window has system chrome, which will always be a little taller by a few pixels.

how is it possible to only permit a native window to resize proportionately in order to always maintain the 2:1 aspect ratio of the stage?

the following code doesn't work as i expect:

package
{
//Imports
import flash.display.NativeWindow;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.NativeWindowBoundsEvent;

//Class
[SWF(width="1000", height="500", frameRate="60", backgroundColor="#000000")]
public class WindowTest extends Sprite
    {
    //Constants
    private static const ASPECT_RATIO:Number = 2.0; //2:1 Aspect Ratio

    //Constructor
    public function WindowTest()
        {
        init();
        }

    //Initialization
    private function init():void
        {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, windowResizeEventHandler);
        }

    //Window Resize Event Handler
    private function windowResizeEventHandler(evt:NativeWindowBoundsEvent):void
        {
        evt.currentTarget.width = stage.stageHeight * ASPECT_RATIO;
        }
    }
}

回答1:

prevent the default event, and resize the window manually:
EDIT: it seems, that air is calculating the width in a weird way, so to prevent flickering in the beginning set the window size to 1050x500 in the SWF tag.

package{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.NativeWindowBoundsEvent;

//Class
[SWF(width="1000", height="500", frameRate="60", backgroundColor="#000000")]
public class airtest extends Sprite
{
    //Constants
    private static const ASPECT_RATIO:Number = 2.0; //2:1 Aspect Ratio

    //Constructor
    public function airtest()
    {
        init();
    }

    //Initialization


private function init():void
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZING, windowResizeEventHandler);
    }

    private function windowResizeEventHandler(evt:NativeWindowBoundsEvent):void
    {
        evt.preventDefault()
        if (evt.beforeBounds.width != evt.afterBounds.width){//user resizes width
            evt.currentTarget.width = evt.afterBounds.width
            evt.currentTarget.height = evt.afterBounds.width/ASPECT_RATIO;
        } else if (evt.beforeBounds.height != evt.afterBounds.height){
            evt.currentTarget.height = evt.afterBounds.height
            evt.currentTarget.width = evt.afterBounds.height*ASPECT_RATIO;
        }

    }
}
}