用户在Adobe AIR中更改屏幕分辨率时如何获取?

时间:2017-11-27 16:39:07

标签: actionscript-3 air desktop

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

像事件一样(例子):

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

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

var resolution:Rectangle = Screen.mainScreen.bounds;

setInterval(function():void {

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

}, 1000);

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

据我所知,轮询(你当前在做什么)是AS3检测屏幕分辨率变化的唯一方法。

但是,如果您处于最大化或全屏窗口,则窗口(或舞台设置为NO_SCALE)将在分辨率更改时触发调整大小事件。 (see the answer from Diniden)。虽然我建议听stage.nativeWindow对象而不是舞台本身。

  

我说你现在正以一种完全可以接受的方式做到这一点。

请注意,如果这是桌面应用程序,则用户可以将您的程序放在不是主要监视器的监视器上(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);