您的位置:首页 > 移动开发 > Android开发

AIR(iOS/Android)程序中控制设备方向

2013-11-08 22:14 381 查看
在Android或iOS平台的大多数项目中,可能只针对横屏或者竖屏来做界面的适配。

在air中,可以用到 StageOrientationEvent.ORIENTATION_CHANGING 这个事件来让程序只接受横屏或竖屏的旋转操作,让程序始终保持横屏或竖屏。

以横屏为例:

package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageOrientation;
import flash.display.StageScaleMode;
import flash.events.StageOrientationEvent;
import flash.text.TextField;
import flash.text.TextFormat;

public class Test extends Sprite
{
private var _label:TextField;

public function Test()
{
super();

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

_label = new TextField();
_label.width = 600;
_label.height = 400;
_label.border = true;
_label.defaultTextFormat = new TextFormat(null, 20, 0);
this.addChild(_label);

stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGING, orientationChangingHandler);
stage.addEventListener(StageOrientationEvent.ORIENTATION_CHANGE, orientationChangeHandler);

_label.text = "Default Orientation: " + stage.orientation;
if(stage.orientation != StageOrientation.ROTATED_LEFT && stage.orientation !=  StageOrientation.ROTATED_RIGHT) {
stage.setOrientation(StageOrientation.ROTATED_RIGHT);
}
}

private function orientationChangingHandler(event:StageOrientationEvent):void
{
if(event.afterOrientation != StageOrientation.ROTATED_LEFT && event.afterOrientation !=  StageOrientation.ROTATED_RIGHT) {
event.preventDefault();//阻止设备旋转到竖屏
_label.appendText("\n不允许旋转到: " + event.afterOrientation);
}
}

private function orientationChangeHandler(event:StageOrientationEvent):void
{
_label.appendText("\n设备已选择至: " + event.afterOrientation);
}
//
}
}


另外,还需要将autoOrients设置为true,可以设置
app.xml: <initialWindow><autoOrients>true</autoOrients></initialWindow>

或: stage.autoOrients = true;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息