Android Lock Screen Orientation
一些与屏幕有关的基础知识: //这个是手机屏幕的旋转角度
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的更多相关文章
- HTML5: Screen Orientation API
媒体的询问取决于智能手机和平板布局调整的方向一致网站.但有时候你被锁定在一个希腊网站特定方向.横向或纵向.此时,是本机格式可以指定保健应用. APP只显示在一个预设格式-独立于实际设备方向.通过使用H ...
- 查看,设置,设备的 竖屏-横屏模式 screen.orientation
<body> <div id="doc"></div> <div id="model"></div> ...
- App/Activity/Screen Orientation
测试android屏幕方向的小Demo 1.首先我们在values下面新建文件arrays.xml(用来在下拉列表中显示) <?xml version="1.0" encod ...
- 屏幕方向读取与锁定:Screen Orientation API(转)
什么是 Screen Orientation API Screen Orientation API 为 Web 应用提供了读取设备当前屏幕方向.旋转角度.锁定旋转方向.获取方向改变事件的能力.使得特定 ...
- [Android Pro] 横竖屏切换时,禁止activity重新创建,android:configChanges="keyboardHidden|orientation" 不起作用
referece to : http://blog.csdn.net/mybook1122/article/details/24978025 这个网上搜索,很多结果都是: AndroidManifes ...
- Windows Phone Bing lock screen doesn't change解决方法
之前一直用的Lumia 925,Bing lock screen每天都会更换.这几天换了Lumia 930,同步了账号相关的设置,发现Bing lock screen不再每天更换.尝试重启.使用cel ...
- 【转】ANDROID LOLLIPOP SCREEN CAPTURE AND SHARING
https://datatheorem.github.io/android/2014/12/26/android-screencapture/ https://www.youtube.com/watc ...
- Android Screen Orientation
Ref:Android横竖屏切换小结 Ref:Android游戏开发之横竖屏的切换(二十七)
- Android Screen Orientation Change (Screen Rotation) Example
原文见: http://techblogon.com/android-screen-orientation-change-rotation-example/#
随机推荐
- http://blog.csdn.net/wxwzy738/article/details/16968767
http://blog.csdn.net/wxwzy738/article/details/16968767
- RHadoop计算平台搭建
原创文章,转载请注明: 转载自www.cnblogs.com/tovin/p/3824554.html 本文基于CentOS6.4系统介绍基于RHadoop平台的搭建,Hadoop的搭建可以参考ht ...
- iOS手势学习UIGestureRecognizer & cocos2d 手势推荐
iOS手势学习UIGestureRecognizer & cocos2d 手势推荐 手势识别类型: UILongPressGestureRecognizer // 长按UIPanGestur ...
- 华为OJ:字符串加解密
题目描述 1.对输入的字符串进行加解密,并输出. 2加密方法为: 当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B:字母Z时则替换为a: 当内容是数字时则把该 ...
- interviewbit :Min Steps in Infinite GridBookmark Suggest Edit
You are in an infinite 2D grid where you can move in any of the 8 directions : (x,y) to (x+1, y), (x ...
- AngularJS学习笔记1——什么是AngularJS?
Angular JS是一个由Google维护的开源的Javascript框架,主要作者为: Misko Hevery(angular JS之父, Sr. Computer Scientist at G ...
- iOS keyChain
keychain在ios中是保存在sqlite数据库中的. 这个数据库文件的位置: 真机: /private/var/Keychains/keychain-2.db 虚拟机: /Users/USER- ...
- Highcharts属性详解
Highcharts的基本属性和方法详解 Highcharts 是一个用纯JavaScript编写的一个图表库, 能够很简单便捷的在web网站或是web应用程序添加有交互性的图表,并且免费提供给个人学 ...
- Android中通过导入静态数据库来提高应用第一次的启动速度
一个Android应用给用户的第一印象非常重要,除了要有好的创意和美观的界面,性能也是很关键的部分,本文讨论的就是第一次启动的速度问题. Android应用的启动过程不能让用户等待太长时间,个人觉得最 ...
- IE内存泄露与无法回收研究小结
一.内存泄露 之前确实看了很多资料,但这位大哥的话可谓画龙点睛,不是奉承他,一下子就打通了我的任督二脉,请看: trarck 写道 IE下的内存泄露原因就是循环引用,IE的垃圾回收器不能很 ...