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

本文介绍如何使用 StageOrientationEvent.ORIENTATION_CHANGING 事件在 Adobe AIR 应用中限制屏幕旋转,仅允许横屏显示,并展示了如何通过代码实现这一功能。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值