手动方向AIR 2.6 和更高版本 可以使用舞台的 setOrientation() 或 setAspectRatio() 方法控制舞台方向。 设置舞台方向可以使用 Stage 对象的 setOrientation() 方法在运行时设置舞台方向。使用 StageOrientation 类指定的字符串常量指定所需的方向: this.stage.setOrientation( StageOrientation.ROTATED_RIGHT ); 并不是所有设备和操作系统都支持每个可能的方向。例如,Android 2.2 在纵向标准设备上不支持以编程方式选择向左旋转方向,并且根本不支持向下翻转方向。舞台的 supportedOrientations 属性提供了一个可以传递到 setOrientation() 方法的方向列表:
var orientations:Vector.<String> = this.stage.supportedOrientations;
for each( var orientation:String in orientations )
{
trace( orientation );
}
设置舞台高宽比如果您非常在意舞台的高宽比,则可以将高宽比设置为纵向或横向。可以使用舞台的 setAspectRatio() 方法在 AIR 应用程序描述符中或在运行时设置高宽比: this.stage.setAspectRatio( StageAspectRatio.LANDSCAPE ); 运行时为指定的高宽比选择两个可能的方向之一。这可能与当前设备的方向不匹配。优先选择默认方向而不是向下翻转方向。优先选择适用于滑出键盘的方向而不是相反方向。 示例:将舞台方向设置为与设备方向相匹配下面的示例演示了更新舞台方向以匹配当前设备方向的功能。舞台的 deviceOrientation 属性指示设备的物理方向,即使关闭了自动方向也会指示。 function refreshOrientation( theStage:Stage ):void
{
switch ( theStage.deviceOrientation )
{
case StageOrientation.DEFAULT:
theStage.setOrientation( StageOrientation.DEFAULT );
break;
case StageOrientation.ROTATED_RIGHT:
theStage.setOrientation( StageOrientation.ROTATED_LEFT );
break;
case StageOrientation.ROTATED_LEFT:
theStage.setOrientation( StageOrientation.ROTATED_RIGHT );
break;
case StageOrientation.UPSIDE_DOWN:
theStage.setOrientation( StageOrientation.UPSIDE_DOWN );
break;
default:
//No change
}
}
方向更改是异步执行的。可以侦听舞台调度的 orientationChange 事件以检测更改的完成情况。如果某个设备不支持某个方向,setOrientation() 调用将失败且不引发错误。 |
|