简体   繁体   English

Flex 4.5 Mobile iOS 确定实际屏幕/舞台分辨率的问题

[英]Flex 4.5 Mobile iOS problems with determining actual screen/stage resolution

I am having a fairly huge problem.我有一个相当大的问题。 I am hoping this is due to my own stupidity and not a bug of some sort.我希望这是由于我自己的愚蠢而不是某种错误。 I have code that needs to know the screen dimensions.我有需要知道屏幕尺寸的代码。 I have not found a reliable way to do that.我还没有找到可靠的方法来做到这一点。 Using production release of Flash Builder 4.5 on iPad 2 and iPhone4/iPod Touch 4 iOS devices.在 iPad 2 和 iPhone4/iPod Touch 4 iOS 设备上使用 Flash Builder 4.5 的生产版本。 In general my app works great but I can't determine the screen size and orientation at program start time.一般来说,我的应用程序运行良好,但我无法在程序启动时确定屏幕大小和方向。 Let me explain the problems I am having:让我解释一下我遇到的问题:

On entry into the "init" function, the one called by the ADDED_TO_STAGE event, the values of stage.stageHeight and stage.stageWidth are both 0.在进入由 ADDED_TO_STAGE 事件调用的“init”function 时,stage.stageHeight 和 stage.stageWidth 的值都是 0。

I can query Capabilities.screenResolutionX and Capabilities.screenResolutionY, but they are WRONG.我可以查询 Capabilities.screenResolutionX 和 Capabilities.screenResolutionY,但它们是错误的。 They have the raw X and Y values, but regardless of the orientation.它们具有原始 X 和 Y 值,但与方向无关。 So for example I start in landscape mode by screenResolutionX contains 768 (or whatever) instead of 1024.因此,例如,我通过 screenResolutionX 以横向模式开始包含 768(或其他)而不是 1024。

I look at the values of stage.width and stage.height and they don't have valid values.我查看了 stage.width 和 stage.height 的值,它们没有有效值。

I have an onResize function setup for EVENT.RESIZE, but it doesn't get called if the app is started from the device when it is in landscape mode already.我有一个用于 EVENT.RESIZE 的 onResize function 设置,但如果应用程序在已经处于横向模式时从设备启动,则不会调用它。 If I start the app in portrait mode and then rotate, this does get called.如果我以纵向模式启动应用程序然后旋转,这会被调用。

So my question is what should I query right at the startup of the app to know the real width and height of the app.所以我的问题是我应该在应用程序启动时查询什么来了解应用程序的实际宽度和高度。 There must be a way to do this but apparently not using any of the methods above!必须有办法做到这一点,但显然不使用上述任何方法!

By the way, this is on iOS devices.顺便说一句,这是在 iOS 设备上。 I can't say how it works on others.我不能说它对其他人有何影响。 I have confirmed these results both by printing out the results and by running it in the debugger.我已经通过打印结果和在调试器中运行它来确认这些结果。

Short answer:简短的回答:

The most easy way is to use the stage.fullScreenWidth and stage.fullScreenHeight properties.最简单的方法是使用stage.fullScreenWidthstage.fullScreenHeight属性。

They correctly report the screen size for any orientation and any platforms at startup time (no need to wait for ADDED_TO_STAGE or RESIZE events).它们在启动时正确报告任何方向和任何平台的屏幕尺寸(无需等待 ADDED_TO_STAGE 或 RESIZE 事件)。

More details:更多细节:

On Android and Blackberry , AIR reports the same (and correct values) at any time when using any of the following properties:AndroidBlackberry 上,AIR 在使用以下任何属性时随时报告相同(和正确的值):

  • stage.stageWidth & stage.stageHeight stage.stageWidth & stage.stageHeight
  • Screen.mainScreen.bounds.width & Screen.mainScreen.bounds.height Screen.mainScreen.bounds.width & Screen.mainScreen.bounds.height
  • stage.fullScreenWidth & stage.fullScreenHeight stage.fullScreenWidth & stage.fullScreenHeight

On iOS , at startup, the three syntaxes give three different results (the following example values are for an application running on iPad2 in landscape mode):iOS 上,在启动时,三种语法给出三种不同的结果(以下示例值适用于在 iPad2 上以横向模式运行的应用程序):

  • stage.stageWidth/Height: the default stage size when launching a swf (500x375) stage.stageWidth/Height:启动 swf 时的默认舞台大小(500x375)
  • Screen.mainScreen.bounds: the physical resolution of the screen in portrait mode (768x1024) screen.mainScreen.bounds:竖屏时屏幕的物理分辨率(768x1024)
  • stage.fullScreenWidth/Height: the correct screen resolution in the given orientation. stage.fullScreenWidth/Height:给定方向的正确屏幕分辨率。 (1024x768 - correct) (1024x768 - 正确)

Note that on iOS, the RESIZE event is fired two times when the application starts, but the values are correct only the second time (stage.stageWidth == stage.fullScreenWidth).请注意,在 iOS 上,应用程序启动时会触发两次 RESIZE 事件,但只有第二次值正确(stage.stageWidth == stage.fullScreenWidth)。

I have previously had issues with stageWidth and stageHeight not giving proper values immediately on startup, an easy way to get around this is to wait a frame or two before checking them.我以前遇到过 stageWidth 和 stageHeight 在启动时没有立即给出正确值的问题,解决这个问题的一种简单方法是在检查它们之前等待一两帧。

One option is to delay the initialization of your app, something along the line of this:一种选择是延迟应用程序的初始化,大致如下:

private var _startup_delay:int = 10;

public function Constructor(){
    addEventListener(Event.ENTER_FRAME, handleEnterFrame);
}

public function handleEnterFrame(e:Event):void{
    _startup_delay--;
   if(_startup_delay <= 0){
       init();
       removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
    }
}

Another option is to instead dispatch a fake resize event and let your previous rotation code deal with it once the delay has passed:另一种选择是发送一个假的 resize 事件,并在延迟过去后让您之前的轮换代码处理它:

public function handleEnterFrame(e:Event):void{
    _startup_delay--;
   if(_startup_delay <= 0){
       stage.dispatchEvent(new Event(Event.RESIZE));
       removeEventListener(Event.ENTER_FRAME, handleEnterFrame);
   }
}

on App Start, would在 App Start 上,会

public function Main()
{
    loaderInfo.addEventListener(Event.COMPLETE, _onComplete_loaderInfo);
}

private function _onComplete_loaderInfo(__e:Event):void
{
    // I should now have access to stage.stageWidth/stage.stageHeight.
}

work for getting the screen res?工作以获得屏幕资源? I would like to state, I have done NO research into this, I was just searching for something else and came across your problem and just commenting Aloud.我想 state,我没有对此进行任何研究,我只是在寻找其他东西,遇到了你的问题,只是大声评论。 Now, I am not saying they will be correct values, but they should atleast NOT be zero.现在,我并不是说它们将是正确的值,但它们至少应该为零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM