一些与屏幕有关的基础知识:

//这个是手机屏幕的旋转角度
final int rotation = this.getWindowManager().getDefaultDisplay().getOrientation(); rotation值有:
Surface.ROTATION_0
Surface.ROTATION_90
Surface.ROTATION_180
Surface.ROTATION_270 //这个是读取当前Activity 的屏幕方向
final int orientation = this.getResources().getConfiguration().orientation; orientation值有:
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_LANDSCAPE //这个是设置Activity 的屏幕方向,与AndroidManifest.xml中的android:screenOrientation="landscape"配置等同
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

  

Build.VERSION:Various version strings.

android.os.Build.VERSION.SDK_INT:The user-visible SDK version of the framework; its possible values are defined in Build.VERSION_CODES.

Build.VERSION_CODES:Enumeration of the currently known SDK version codes. These are the values that can be found in SDK. Version numbers increment monotonically with each official platform release.

android.os.Build.VERSION_CODES.FROYO:June 2010: Android 2.2

The REVERSE_LANDSCAPE and REVERSE_PORTRAIT appear after android 2.3,so we can not use them on android 2.2 and former devices.

private void disableRotation()
{
final int orientation = getResources().getConfiguration().orientation;
final int rotation = getWindowManager().getDefaultDisplay().getOrientation(); // Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
{
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270)
{
if (orientation == Configuration.ORIENTATION_PORTRAIT){
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
}
}

enable the rotation:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

About the rotation and orientation,on some devices,the Surface.ROTATION_0 corresponds to ORIENTATION_LANDSCAPE,but others,may ORIENTATION_PORTRAIT.So we have to so some work to judge it.

However,for some reason ROTATION_90 corresponds to SCREEN_ORIENTATION_REVERSE_PORTRAIT on the Xoom if you use the above method.So another method appears:

 private void disableRotation()
{
final int orientation = getResources().getConfiguration().orientation;
final int rotation = getWindowManager().getDefaultDisplay().getOrientation(); // Copied from Android docs, since we don't have these values in Froyo 2.2
int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8;
int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO)
{
SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} if (orientation == Configuration.ORIENTATION_PORTRAIT) {
if (rotation == Surface.ROTATION_0|| rotation == Surface.ROTATION_270)
// 0 for phone, 270 for tablet
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else {
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
} else {
if (rotation == Surface.ROTATION_90|| rotation == Surface.ROTATION_0)
// 90 for phone, 0 for tablet
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
}
} }

i'm not sure the note "//0 for phone, 270 for tablet" and "// 90 for phone, 0 for tablet" are right on any device,but at least it works well in my device.so I use the latter one.

When you want to disable/enable the autorotation of the device ,you have to write the settings.

Android Lock Screen Orientation的更多相关文章

  1. HTML5: Screen Orientation API

    媒体的询问取决于智能手机和平板布局调整的方向一致网站.但有时候你被锁定在一个希腊网站特定方向.横向或纵向.此时,是本机格式可以指定保健应用. APP只显示在一个预设格式-独立于实际设备方向.通过使用H ...

  2. 查看,设置,设备的 竖屏-横屏模式 screen.orientation

    <body> <div id="doc"></div> <div id="model"></div> ...

  3. App/Activity/Screen Orientation

    测试android屏幕方向的小Demo 1.首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示) <?xml version="1.0" encod ...

  4. 屏幕方向读取与锁定:Screen Orientation API(转)

    什么是 Screen Orientation API Screen Orientation API 为 Web 应用提供了读取设备当前屏幕方向.旋转角度.锁定旋转方向.获取方向改变事件的能力.使得特定 ...

  5. [Android Pro] 横竖屏切换时,禁止activity重新创建,android:configChanges="keyboardHidden|orientation" 不起作用

    referece to : http://blog.csdn.net/mybook1122/article/details/24978025 这个网上搜索,很多结果都是: AndroidManifes ...

  6. Windows Phone Bing lock screen doesn't change解决方法

    之前一直用的Lumia 925,Bing lock screen每天都会更换.这几天换了Lumia 930,同步了账号相关的设置,发现Bing lock screen不再每天更换.尝试重启.使用cel ...

  7. 【转】ANDROID LOLLIPOP SCREEN CAPTURE AND SHARING

    https://datatheorem.github.io/android/2014/12/26/android-screencapture/ https://www.youtube.com/watc ...

  8. Android Screen Orientation

    Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)

  9. Android Screen Orientation Change (Screen Rotation) Example

    原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#

随机推荐

  1. C#格式化输出

    double a = 12354.365; Console.WriteLine(string.Format("{0:f4}", a)); 输出a的四位小数

  2. tcp抓包 Wireshark 使用

    fidder主要是针对http(s)协议进行抓包分析的,所以类似wireshark/tcpdump这种工作在tcp/ip层上的抓包工具不太一样,这种工具一般在chrome/firefox的开发者工具下 ...

  3. iOS开发--Bison详解连连支付集成简书

    "最近由于公司项目需要集成连连支付,文档写的不是很清楚,遇到了一些坑,因此记录一下,希望能帮到有需要的人." 前面简单的集成没有遇到什么坑,在此整理一下官方的集成文档,具体步骤如下 ...

  4. 一个简单的以User权限启动外部应用程序(用NetUserAdd函数和USER_INFO_1结构体动态添加用户,然后用CreateProcessWithLogonW启动程序)

    版权声明:本文为博主原创文章,未经博主允许不得转载. BOOL ExecuteAsUser(LPCWSTR lpszUserName, LPCWSTR lpszPassword, LPCWSTR lp ...

  5. Spring笔记——使用Spring进行面向切面(AOP)编程

    要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...

  6. 267. Palindrome Permutation II

    题目: Given a string s, return all the palindromic permutations (without duplicates) of it. Return an ...

  7. iScrolljs 模拟android相册

    var showBigImage = new iScroll('showBigImage', { snap: 'div', snapThreshold: 480, momentum: false, z ...

  8. backbone todo example

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. 基于webpivottable做的透视表

    1.绑定数据和配置: var wptConfig=<%= wptConfig%>; webPivotTable.setCsvData(<%=dataFields %>, < ...

  10. Java-HTTP连接时如何使用代理(一)—— System.Property方式

    在发起HTTP请求(openConnection() 或者 openStream())之前,加上以下2行代码: System.setProperty("proxyHost", PR ...