转屏的处理

本文介绍如何在Android应用中锁定屏幕方向,防止设备倾斜时自动旋转。提供了锁定和检测当前方向的代码示例,并展示了如何在执行特定操作时临时锁定方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

出自http://eigo.co.uk/News-Article.aspx?NewsArticleID=103

How to lock the orientation

In the onCreateDialog(int) event of the activity use the setRequestedOrientation(int) method to set the screen orientation to your chosen setting. The activity will stay in this orientation regardless of if the device is tilted or not.

[Code sample – How to lock the orientation]
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState
{
    super.onCreate(savedInstanceState);
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

 

How to detect the current orientation

To programmatically detect the current orientation of the activity use the following code snippet. The orientation property of the Configuration class returns three possible values corresponding to Landscape, Portrait and Square.

[Code sample – How to detect the current orientation]
switch (this.getResources().getConfiguration().orientation)
{
case Configuration.ORIENTATION_PORTRAIT:
  // Do something here
  break;
case Configuration.ORIENTATION_LANDSCAPE:
  // Do something here
  break;
case Configuration.ORIENTATION_SQUARE:
  // Do something here
  break;
default:
  throw new Exception("Unexpected orientation enumeration returned");
  break;
}

 

Example : Locking rotation while performing an action.

You might wish to disable the screen rotation whilst performing an action or by user command, to do this you need to combine the above samples to detect the current orientation and lock the display to that orientation.

[Code sample – Locking rotation while performing an action]
// Sets screen rotation as fixed to current rotation setting
private void mLockScreenRotation()
{
  // Stop the screen orientation changing during an event
    switch (this.getResources().getConfiguration().orientation)
    {
  case Configuration.ORIENTATION_PORTRAIT:
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    break;
  case Configuration.ORIENTATION_LANDSCAPE:
    this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    break;
    }
}

Once your action has completed you may wish to enable screen rotation again, see the next section for an example on how to do this.

 

How to re-enable screen rotation

To enable the orientation to be automatically changed on device tilt simply pass thesetRequestedOrientation(int) method the enumeration value for an unspecified orientation.

[Code sample – How to re-enable screen rotation]
// allow screen rotations again
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

异步处理 的时候如果转屏 异步处理就会停止,附件就是防止这种情况发生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值