4

我有一个希望锁定为横向的 AIR 应用程序。我从不希望应用程序旋转到纵向模式。Adobe 对这个问题的解决方案似乎是如下配置我的 *-app.xml 文件:

<!-- 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,在 Nook 平板电脑上运行),如果我的设备屏幕在 Flash Builder 打包和部署我的 APK 时关闭,则应用程序以半中断状态启动,其纵横比是横向的,但一切都是用纵向尺寸测量的。在其他设备上,例如我的 Google Nexus (Android 4.1),应用程序可以正确启动(在横向模式下),但如果我关闭屏幕,将设备旋转到纵向模式,然后重新打开屏幕 - 我的 AIR 应用程序一直不可见再次旋转到纵向模式。更糟糕的是,甚至没有任何 StageOrientationEvent.ORIENTATION_CHANGE 事件被调度以表明这已经发生。

我尝试了上述方法的一种变体,即以编程方式设置设备方向:

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

这种方法具有与上述相同的问题。

我也尝试过这个其他 Stack Overflow 线程中提到的方法。我没有禁用自动定向,而是尝试收听 StageOrientationEvent.ORIENTATION_CHANGE 并阻止设备进入纵向模式:

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

这似乎也只在某些时候起作用。在许多情况下, event.preventDefault() 无效,并且应用程序无论如何都会旋转。

所以......我对其他 AIR 开发人员的问题是:有没有人知道可靠地将设备锁定到单一方向的方法?

4

1 回答 1

4

这是我发现在 Android 2.3(Fire and Nook)和 4.1(Nexus 7)上对我有用的方法。我还没有测试更多的设备,但到目前为止它似乎很有希望。

首先,我将我的 -app.xml 配置设置如下:

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

然后我将以下代码添加到我的 spark 应用程序中:

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 );
        }
    }
}
于 2012-09-05T12:55:13.903 回答