我怎样才能正确地控制舞台方向在Android上使用AIR手机? [英] How can I properly control stage orientation on Android with AIR mobile?

查看:97
本文介绍了我怎样才能正确地控制舞台方向在Android上使用AIR手机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我想锁定横向AIR应用程序。我永远不希望应用到旋转为纵向模式。 Adobe的解决这个问题,貌似,是配置我的* -app.xml文件,如下所示:

I have an AIR application that I wish to lock into landscape orientation. I never want the application to rotate to portrait mode. Adobe's solution to this problem, seemingly, would be to configure my *-app.xml file as follows:

<!-- The initial aspect ratio of the app when launched (either "portrait" or "landscape"). Optional. Mobile only. Default is the natural orientation of the device -->
<aspectRatio>landscape</aspectRatio>
<!-- Whether the app will begin auto-orienting on launch. Optional. Mobile only. Default false -->
<autoOrients>false</autoOrients>

这工作的大部分时间,但并非总是如此。例如,在Android中的某些香料(例如:2.x中,在一个角落平板电脑上运行),如果我的设备的屏幕关闭,而Flash Builder中的打包和部署我的APK,应用程序启动的半断状态下,其长宽比是景观,但一切都已经被测量的画像尺寸。在其他设备上,就像我的谷歌Nexus(安卓4.1),应用程序正确启动(横向模式),但如果我关掉屏幕,旋转设备到肖像模式,并打开屏幕的背面导通我的AIR应用程序已被无形再旋转到肖像模式。更糟的是,没有StageOrientationEvent.ORIENTATION_CHANGE事件甚至可以调度来指示,这已经发生了。

This works most of the time- but not always. For instance, in certain flavors of Android (example: 2.x, running on a Nook Tablet) if my device's screen turns off while Flash Builder is packaging and deploying my APK, the app starts in a semi-broken state where its aspect ratio is landscape but everything has been measured with portrait dimensions. On other devices, like my Google Nexus (Android 4.1), the application launches correctly (in landscape mode) but if I turn off the screen, rotate the device to portrait mode, and turn the screen back on- my AIR app has been invisibly rotated into portrait mode again. What's worse, no StageOrientationEvent.ORIENTATION_CHANGE events even get dispatched to indicate that this has happened.

我已经试过了上述办法,这是通过编程设置设备的方向变体:

I've tried a variant of the above approach, which is to programmatically set the device orientation:

private function onAddedToStage( event:Event ):void {
    stage.autoOrients = false;
    stage.setOrientation( StageOrientation.ROTATED_RIGHT );
}

此方法已上述相同的问题。

This approach has the same problems mentioned above.

我也想尽了办法<一href="http://stackoverflow.com/questions/8900849/properly-$p$pventing-orientation-change-in-flex-mobile-app">mentioned在此其他堆栈溢出线程。而不是禁用自动定向的,我已经试过听StageOrientationEvent.ORIENTATION_CHANGE和$ P $进入纵向模式pventing设备:

I've also tried the approach mentioned in this other Stack Overflow thread. Instead of disabling auto-orient, I've tried listening to StageOrientationEvent.ORIENTATION_CHANGE and preventing the device from entering portrait mode:

private function onAddedToStage( event:Event ):void {
    stage.addEventListener( StageOrientationEvent.ORIENTATION_CHANGE, onStageOrientationChange, true, int.MAX_VALUE );
}

private function onStageOrientationChange( event:StageOrientationEvent ):void {
    switch( event.afterOrientation ) {
        case StageOrientation.DEFAULT:
        case StageOrientation.UPSIDE_DOWN:
        case StageOrientation.UNKNOWN:
            event.preventDefault();
            break;
        case StageOrientation.ROTATED_RIGHT:
        case StageOrientation.ROTATED_LEFT:
            break;
    }
}

这似乎也只能工作在某些时候。在许多情况下,事件。preventDefault()没有任何作用,应用旋转反正。

This, too, seems to work only some of the time. In many cases, event.preventDefault() has no effect and the application rotates anyway.

所以...我的问题给其他AIR开发出有:?没有人知道的一种方式,以可靠的锁定装置成一个单一方向

So... my question to other AIR developers out there is: does anyone know of a way to reliably lock a device into a single orientation?

推荐答案

下面是我找到的工作对我来说,在Android 2.3(消防​​和角落)以及4.1(的Nexus 7)的方法。我没有更多的设备测试过,但到目前为止,似乎有希望的。

Here's the approach I've found to work for me, on Android 2.3 (Fire and Nook) as well as 4.1 (Nexus 7). I haven't tested with more devices yet but so far it seems promising.

首先,我已经把我的-app.xml配置如下:

First, I've set my -app.xml configuration as follows:

<aspectRatio>landscape</aspectRatio>
<autoOrients>false</autoOrients>

然后,我已经添加了以下code到我的火花应用:

Then I've added the following code to my spark Application:

private function onAddedToStage():void {
    stage.addEventListener( Event.RESIZE, onStageResize );
    NativeApplication.nativeApplication.addEventListener( Event.ACTIVATE, onNativeApplicationActivate );
}

private function onStageResize( event:Event ):void {
    checkForOrientationChange();
}

private function onNativeApplicationActivate( event:Event ):void {
    checkForOrientationChange();
}

private function checkForOrientationChange():void {
    if ( height > width ) {
        if ( stage ) {
            stage.setOrientation( StageOrientation.ROTATED_RIGHT );
        } else {
            // The first ACTIVATE event occurs before the Application has been added to the stage
            callLater( checkForOrientationChange );
        }
    }
}

这篇关于我怎样才能正确地控制舞台方向在Android上使用AIR手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆