当用户更改 Adob​​e AIR 中的屏幕分辨率时如何获取?

How to get when user change the screen resolution in Adobe AIR?

我可以在我的应用程序启动时获得屏幕分辨率,但我可以知道用户何时更改了分辨率吗?

类似事件(示例):

Screen.mainScreen.addEventListener("CHANGE_RESOLUTION", ...)

我试过使用 setInterval 来监控分辨率,但这是最好的方法吗?

var resolution:Rectangle = Screen.mainScreen.bounds;

setInterval(function():void {

    if(!Screen.mainScreen.bounds.equals(resolution)) {
        trace("changed!");
    }

}, 1000);

这就是我处理 window 调整大小事件的方式。您查看给定 window 的阶段,了解它的大小是如何调整的。其他属性是使其以合理的方式调整大小。

this.stage.scaleMode = StageScaleMode.NO_SCALE;
this.stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, stageResized);

据我所知,轮询(您当前正在做的事情)是 AS3 中检测屏幕分辨率变化的唯一方法。

但是,如果您处于 最大化或全屏 window,window(或设置为 NO_SCALE 的阶段)将在分辨率更改时触发 resize 事件。 ()。尽管我建议监听 stage.nativeWindow 对象而不是舞台本身。

I'd say that you are doing it a perfectly acceptable way currently.

不过请记住,如果这是一个桌面应用程序,用户可能会将您的程序安装在不是主显示器的显示器上 (Screen.mainScreen)。为了支持这种情况,您需要执行以下操作:

//a constant to use for a custom event
const RESOLUTION_CHANGE:String = "resolution_change";

var resolution:Rectangle = curScreen.bounds;

//Adobe recommends using timers instead of setInterval - using ENTER_FRAME may be too expensive for your needs, but would afford the quickest reaction time.
var resolutionTimer:Timer = new Timer(1000); //poll every second (adjust to optimum performance for your application)
resolutionTimer.addEventListener(TimerEvent.TIMER, pollScreenResolution);
resolutionTimer.start();

//get the screen that the window is on
function get curScreen():Screen {
    //get the first screen [0] that the current window is on
    return Screen.getScreensForRectangle(stage.nativeWindow.bounds)[0];
}

function pollScreenResolution(e:TimerEvent) {
    if(!curScreen.bounds.equals(resolution)) {
        trace("changed!");
        //dispatch our custom event
        stage.dispatchEvent(new Event(RESOLUTION_CHANGE));
        resolution = curScreen.bounds; //set the resolution to the new resolution so the event only dispatches once.
    }

}

现在您可以在应用程序的其他部分侦听该事件。

stage.addEventListener(MovieClip(root).RESOLUTION_CHANGE, doSomething);