获取屏幕的方向:

android.view.WindowManager manager = context.getSystemService(Context.WINDOW_SERVICE);

int rotation = manager.getDefaultDisplay().getRotation();

rotation的值为Surface.ROTATION_90或Surface.ROTATION_270时表示竖屏

rotation的值为Surface.ROTATION_0或者Surface.ROTATION_180表示横屏

如何改变Android默认的横竖屏,修改源码一个地方就可以了。

[java] view plaincopy
  1. public int rotationForOrientationLw(int orientation, int lastRotation,
  2. boolean displayEnabled) {
  3. if (mPortraitRotation < 0) {
  4. // Initialize the rotation angles for each orientation once.
  5. Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
  6. .getDefaultDisplay();
  7. if (d.getWidth() > d.getHeight()) {
  8. mPortraitRotation = Surface.ROTATION_90;
  9. mLandscapeRotation = Surface.ROTATION_0;
  10. mUpsideDownRotation = Surface.ROTATION_270;
  11. mSeascapeRotation = Surface.ROTATION_180;
  12. } else {
  13. mPortraitRotation = Surface.ROTATION_0;
  14. mLandscapeRotation = Surface.ROTATION_90;
  15. mUpsideDownRotation = Surface.ROTATION_180;
  16. mSeascapeRotation = Surface.ROTATION_270;
  17. }
  18. }
  19. {
  20. Log.i(TAG, "MediaPlayer.is not PlayingVideo");
  21. synchronized (mLock) {
  22. switch (orientation) {
  23. case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
  24. //always return portrait if orientation set to portrait
  25. //return mPortraitRotation;
  26. return mUpsideDownRotation;
  27. case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
  28. //always return landscape if orientation set to landscape
  29. return mLandscapeRotation;
  30. case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
  31. //always return portrait if orientation set to portrait
  32. //return mUpsideDownRotation;
  33. return mPortraitRotation;
  34. case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
  35. //always return seascape if orientation set to reverse landscape
  36. return mSeascapeRotation;
  37. case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
  38. //return either landscape rotation based on the sensor
  39. mOrientationListener.setAllow180Rotation(
  40. isLandscapeOrSeascape(Surface.ROTATION_180));
  41. return getCurrentLandscapeRotation(lastRotation);
  42. case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
  43. mOrientationListener.setAllow180Rotation(
  44. !isLandscapeOrSeascape(Surface.ROTATION_180));
  45. return getCurrentPortraitRotation(lastRotation);
  46. }
  47. mOrientationListener.setAllow180Rotation(
  48. orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
  49. || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
  50. // case for nosensor meaning ignore sensor and consider only lid
  51. // or orientation sensor disabled
  52. //or case.unspecified
  53. if (mLidOpen) {
  54. return mLidOpenRotation;
  55. } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
  56. return mCarDockRotation;
  57. } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
  58. return mDeskDockRotation;
  59. } else {
  60. if (useSensorForOrientationLp(orientation)) {
  61. return mOrientationListener.getCurrentRotation(lastRotation);
  62. }
  63. return Surface.ROTATION_0;
  64. }
  65. }
  66. }
  67. }

修改上面倒数一行代码把return Surface.ROTATION_0改为你要的方向,记得这个要和上面的匹配,宽高不同,Surface.ROTATION_0也不同。因为代码开头就有

[java] view plaincopy
  1. if (mPortraitRotation < 0) {
  2. // Initialize the rotation angles for each orientation once.
  3. Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
  4. .getDefaultDisplay();
  5. if (d.getWidth() > d.getHeight()) {
  6. mPortraitRotation = Surface.ROTATION_90;
  7. mLandscapeRotation = Surface.ROTATION_0;
  8. mUpsideDownRotation = Surface.ROTATION_270;
  9. mSeascapeRotation = Surface.ROTATION_180;
  10. } else {
  11. mPortraitRotation = Surface.ROTATION_0;
  12. mLandscapeRotation = Surface.ROTATION_90;
  13. mUpsideDownRotation = Surface.ROTATION_180;
  14. mSeascapeRotation = Surface.ROTATION_270;
  15. }
  16. }

调试android记录 —— 屏幕改为竖屏

http://hi.baidu.com/jsxhxcq/item/960ca20607ed24e1359902d2

在调试android时,项目需要将屏幕竖屏,而且没有传感器。

按照网上的要求 在 build/target/product/core.mk 中

PRODUCT_POLICY := android.policy_phone   //mid 改为phone

但是改完没有任何反应。随就追踪下去

1. 系统启动后 执行 performEnableScreen()

performEnableScreen() -->     mPolicy.enableScreenAfterBoot();->

windowmanagerservice.Java
    public void enableScreenAfterBoot() {
        synchronized(mWindowMap) {
            if (mSystemBooted) {
                return;
            }
            mSystemBooted = true;
        }
2. 在其函数中调用updateRotation();

public void enableScreenAfterBoot() {
        readLidState();
        updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE);
    }

void updateRotation(int animFlags) {
        mPowerManager.setKeyboardVisibility(mLidOpen);
        int rotation = Surface.ROTATION_0;

if (mLidOpen) {
            rotation = mLidOpenRotation;
        } else if (mDockState == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {
            rotation = mCarDockRotation;
        } else if (mDockState == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {
            rotation = mDeskDockRotation;
        }
        //if lid is closed orientation will be portrait
        try {
            //set orientation on WindowManager
            mWindowManager.setRotation(rotation, true,
                    mFancyRotationAnimation | animFlags);
        } catch (RemoteException e) {
            // Ignore
        }
    }

3. setRotation()函数在 windowmanagerservices.java  中

static final boolean DEBUG_ORIENTATION = true; //打开调试信息

设置旋转函数, 下面调用关系

setRotation()  - > setRotationUnchecked()   - >  setRotationUncheckedLocked()->   rotationForOrientationLw()

if (useSensorForOrientationLp(orientation)) {
                    // If the user has enabled auto rotation by default, do it.
                    int curRotation = mOrientationListener.getCurrentRotation();
                    return curRotation >= 0 ? curRotation : lastRotation;
                }

让所有应用都横屏显示

http://blog.csdn.net/knock/article/details/7629585

[java] view plaincopy
  1. frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
  2. public int rotationForOrientationLw(int orientation, int lastRotation,
  3. boolean displayEnabled) {
  4. // Initialize the rotation angles for each orientation once.
  5. Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
  6. .getDefaultDisplay();
  7. if (d.getWidth() > d.getHeight()) {
  8. mPortraitRotation = Surface.ROTATION_0;  //jeff. ROTATION_90;
  9. mLandscapeRotation = Surface.ROTATION_0;
  10. mUpsideDownRotation = Surface.ROTATION_90;  //jeff. 270;
  11. mSeascapeRotation = Surface.ROTATION_180;
  12. }

android屏幕旋转在framework中的修改

http://blog.csdn.net/xulinguestc/article/details/6435957

在framework中修改,可以随意修改屏幕0°指向的方向,其实也是framework层做的映射。 修改HAL层来修改屏幕0°指向的方向应该也是可以的,还没有试过, 估计会复杂点,应该要修改触摸屏的坐标, 触摸键值映射表, 比较麻烦,其实没什么必要,修改framework层就可以搞定了。

平板电脑一般是默认横屏, 竖屏的APP程序, 会自动旋转90°, 由于是顺时针转90°, 需要改为逆时针转90°; 也就是要把portrait改逆时针转90°,这样就和手机一致,兼容很多gsensor游戏, 修改点如下:

[java] view plaincopy
  1. PhoneWindowManager.java(//192.168.1.4/opt/android_froyo_smdk/frameworks/policies/base/phone/com/android/internal/policy/impl)
  2. public int rotationForOrientationLw(int orientation, int lastRotation,
  3. boolean displayEnabled) {
  4. if (mPortraitRotation < 0) {
  5. // Initialize the rotation angles for each orientation once.
  6. Display d =((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
  7. .getDefaultDisplay();
  8. if (d.getWidth() > d.getHeight()) {
  9. mPortraitRotation = Surface.ROTATION_270;//Surface.ROTATION_90;
  10. mLandscapeRotation =Surface.ROTATION_0;
  11. } else {
  12. mPortraitRotation =Surface.ROTATION_0;
  13. mLandscapeRotation = Surface.ROTATION_270;//Surface.ROTATION_90;
  14. }
  15. }

以下是参考文章------------------------------------------------------------------------------------------------------

本行实现以后才发现,google在1.5到2.2这个过程中改进了很多,1.5修改竖屏比较麻烦,而2.2是相当的容易!
其实基本上google将之前版本的默认为竖屏的做法进行了改进,不需要再花费更多力气在屏幕的默认横竖切换上面。1.还是kernel竖屏,可以显示到屏幕出现"A N D R O I D"字样
  启动参数里加入fbcon=rotate:1    (0:正常屏; 1:顺时钟转90度; 2:转180度; 3:顺时钟转270度;)
最后生成的autoconf.h里有类似项:
#define CONFIG_CMDLINE "console=ttySAC0,115200 fbcon=rotate:1"此项的解析在$(kernel)/drivers/video/console/fbcon.c
static int __init fb_console_setup(char *this_opt);
只是去初始化变量initial_rotation,然后initial_rotation会传递给其他需要的结构。
要选上:Device Drivers -> Graphics support -> Console display driver support ->Framebuffer Console support -> Framebuffer Console Rotation
注意:参考$(kernel)/documentation/fb/fbcon.txt2.android OS旋转屏幕
froyo中已经相当容易,仅修改一处:
frameworks/base/libs/surfaceflinger/SurfaceFlinger.cpp
void GraphicPlane::setDisplayHardware(DisplayHardware *hw)
{
    mHw = hw;    // initialize the display orientation transform.
    // it's a constant that should come from the display driver.
//    int displayOrientation = ISurfaceComposer::eOrientationDefault;
    int displayOrientation = ISurfaceComposer::eOrientation90; //jeff.
    。。。
}
或者只在init.rc中增加一项:
setprop ro.sf.hwrotation 90就这么简单的一修改,就可以全程竖屏显示了!

0

android如何改变系统默认横竖屏方向相关推荐

  1. android DrawerLayout 改变系统默认的灰色

    在使用原生的侧滑栏的时候系统默认的主页显示是灰色的, 想要去掉这个灰色的颜色值 可以设置其颜色值为透明 假如定义 DrawerLayout drawerLayout 那么设置颜色透明的时候可以调用se ...

  2. android横竖屏切换动画,Android应用怎么实现屏幕横竖屏切换功能

    Android应用怎么实现屏幕横竖屏切换功能 发布时间:2020-11-26 15:48:57 来源:亿速云 阅读:121 作者:Leah Android应用怎么实现屏幕横竖屏切换功能?针对这个问题, ...

  3. Android 12.0系统默认设置屏幕永不息屏

    目录 1.概述 2. 系统默认设置屏幕永不休眠功能代码 3. 系统默认设置屏幕永不休眠功能分析和功能实现

  4. 全志H6 Android 7.0 平台 修改横竖屏显示

    **问题描述:**全志H6 Android 7.0 并没有给出横竖屏显示属性的设置,可以自己设置一个persist.sys.rotation属性来控制横竖屏的显示 解决方案: diff --git a ...

  5. Android 10.0 设置默认息屏时间

    1.概述 在10.0的系统产品定制化开发中,由于系统默认的息屏时间过短,所以要求修改默认息屏时间修改也是常见的修改功能, 在系统Settings中屏幕超时会根据默认息屏时间来显示屏幕超时的选项,然后设 ...

  6. Android设为系统默认的短信应用

    要设为系统默认的短信应用首先要配置一下AndroidManifest.xml文件,添加下列: <!-- BroadcastReceiver that listens for incoming S ...

  7. Android App 手机兼容平板横竖屏切换

    Android App兼容平板的开发.希望在手机上是竖屏显示,然后在平板上是横竖切换. /*** 判断是否平板设备* @param context* @return true:平板,false:手机* ...

  8. Android 10.0 系统默认开启wifi

    1.概述 在10.0的系统产品中wifi是默认关闭的,由于的纯wifi的产品,所以需要要求默认打开wifi,开机后直接连wifi就可以了 所以需要找到相关的开关默认打开就可以了,然后开始连接wifi ...

  9. Swift iOS16 设置横竖屏方向

    这里写一个兼容旧版ios的屏幕方向设置,实测了ios12.3 和 16.0.2是可以用的.做个笔记. 1.定义三个类型的屏幕模式设置. enum SCREEN_SET {case set_portca ...

最新文章

  1. 2021年大数据Flink(四十二):​​​​​​​BroadcastState
  2. python3中map函数_解决Python3下map函数的显示问题
  3. 深入理解Golang包导入
  4. 全志 更换Update升级路径 Patch
  5. 《地狱之刃:塞娜的献祭》如何通过人物情感营造恐怖氛围?
  6. 数学--矩阵快速幂详解
  7. C++ 学习之旅(5)——设置Setup文件目录
  8. Elasticsearch7.15.2 mysql8.0.26 logstash-input-jdbc 数据全量索引构建
  9. 在 CSS 中,width 和 height 指的是内容区域的宽度和高度
  10. 编写一个学生类 student,包含的属性有学号、姓名年龄,将所有学生存储在一个数组中
  11. java反射回调函数_java回调函数
  12. 曼大和华威计算机科学预科哪个好,华威预科申请曼彻斯特本科
  13. pandownload软件下载
  14. python代码编辑教程_python教程:pycharm编写代码的方式教学
  15. 红帽linux中文系统下载iso,红帽子9.0版下载-redhat linux 9.0 iso下载 简体中文正式版-IT猫扑网...
  16. ****怎么解决UBUNTU里面VIM编辑器键盘错乱问题****
  17. DASCTF X CBCTF 2022九月挑战赛 dino3d
  18. cadence软件初识
  19. Metasploitable2虚拟机镜像下载地址
  20. SparkStreaming面试题

热门文章

  1. linux中touch命令如何使用方法,Linux系统下touch命令的使用方法
  2. Spannable、Spanned、Editable用法及差别
  3. 微软亚洲工程院院长刘震:未来机器人的发展方向以及挑战
  4. java计算机毕业设计宠物店管理系统(附源码、数据库)
  5. OBS采集黑屏问题解决
  6. Three.js入门 (参考胖达老师)| 大帅老猿threejs特训
  7. 【libdatachannel】streamer与js客户端本机联调3 client流程
  8. 笔译现在用计算机考试了吗,做笔译需要计算机技术很强吗
  9. JMeter压力测试,五年Java开发者小米、阿里面经
  10. 2019-09-10 ~ 2022-01-12 第二段xx科技公司 工作生活总结