AIR - 按比例调整本机窗口大小

时间:2011-03-08 23:38:02

标签: actionscript-3 air resize window

我的应用程序启动时,舞台大小为1000 x 500,宽高比为2:1。原生窗口有系统镶边,总是会高几个像素。

如何只允许原生窗口按比例调整大小以便始终保持舞台的2:1宽高比?

以下代码无法正常工作:

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 个答案:

答案 0 :(得分:2)

阻止默认事件,并手动调整窗口大小:
编辑:似乎空气以奇怪的方式计算宽度,因此为了防止在开始时出现闪烁,请在SWF标签中将窗口大小设置为1050x500。

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;
        }

    }
}
}