android如何改变系统默认横竖屏方向

简介:

http://blog.csdn.net/abc19842008/article/details/7543559

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


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

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

[java]  view plain copy
  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

frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java

    public int rotationForOrientationLw(int orientation, int lastRotation,
            boolean displayEnabled) {
            // Initialize the rotation angles for each orientation once.
            Display d = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
            if (d.getWidth() > d.getHeight()) {
                mPortraitRotation = Surface.ROTATION_0;  //jeff. ROTATION_90;
                mLandscapeRotation = Surface.ROTATION_0;
                mUpsideDownRotation = Surface.ROTATION_90;  //jeff. 270;
                mSeascapeRotation = Surface.ROTATION_180;
            }



android屏幕旋转在framework中的修改

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

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

 

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

PhoneWindowManager.java(//192.168.1.4/opt/android_froyo_smdk/frameworks/policies/base/phone/com/android/internal/policy/impl)


public int rotationForOrientationLw(int orientation, int lastRotation,


boolean displayEnabled) {

 


if (mPortraitRotation < 0) {


// Initialize the rotation angles for each orientation once.


Display d =((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE))


.getDefaultDisplay();


if (d.getWidth() > d.getHeight()) {


mPortraitRotation = Surface.ROTATION_270;//Surface.ROTATION_90;


mLandscapeRotation =Surface.ROTATION_0;


} else {


mPortraitRotation =Surface.ROTATION_0;


mLandscapeRotation = Surface.ROTATION_270;//Surface.ROTATION_90;


}


}

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

本行实现以后才发现,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就这么简单的一修改,就可以全程竖屏显示了!


相关文章
|
24天前
|
搜索推荐 Android开发 iOS开发
安卓与iOS系统的用户界面设计对比分析
本文通过对安卓和iOS两大操作系统的用户界面设计进行对比分析,探讨它们在设计理念、交互方式、视觉风格等方面的差异及各自特点,旨在帮助读者更好地理解和评估不同系统的用户体验。
18 1
|
2月前
|
搜索推荐 Android开发 iOS开发
探析安卓与iOS系统的优劣
【2月更文挑战第7天】安卓与iOS是当今手机市场上最主流的两款操作系统,各有优劣。本文将从用户体验、开放程度、生态系统等方面对两者进行深入探析,以期帮助读者更好地了解它们的特点。
|
2月前
|
Android开发 数据安全/隐私保护 iOS开发
安卓与iOS系统的发展趋势与比较分析
【2月更文挑战第6天】 在移动互联网时代,安卓和iOS系统作为两大主流移动操作系统,各自呈现出不同的发展趋势。本文将从技术角度出发,对安卓和iOS系统的发展方向、特点及未来趋势进行比较分析,以期为读者提供更深入的了解和思考。
34 4
|
3月前
|
Linux 调度 Android开发
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
46 0
|
4月前
|
数据库 Android开发
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
Android Studio开发之应用组件Application的讲解及实战(附源码,通过图书管理信息系统实战)
49 0
|
4月前
|
JSON 自然语言处理 Java
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
Android App开发语音处理之系统自带的语音引擎、文字转语音、语音识别的讲解及实战(超详细 附源码)
120 0
|
18天前
|
机器学习/深度学习 人工智能 搜索推荐
探索安卓应用中的新趋势:人工智能驱动的智能推荐系统
传统的应用推荐系统已经无法满足用户日益增长的个性化需求。本文将探讨如何通过引入人工智能技术,构建智能推荐系统,为用户提供更加精准、个性化的应用推荐体验,进而提升应用的用户满意度和留存率。
16 0
|
1月前
|
搜索推荐 测试技术 定位技术
基于Android的自助导游系统的设计与实现(论文+源码)_kaic
基于Android的自助导游系统的设计与实现(论文+源码)_kaic
|
1月前
|
搜索推荐 安全 Android开发
安卓与iOS系统的用户体验比较
【2月更文挑战第11天】 在当今移动设备市场上,安卓和iOS系统一直是两大主流操作系统。本文将从用户界面设计、应用生态、系统定制性等方面对安卓和iOS系统进行比较分析,旨在探讨两者的优势和劣势,为用户选择合适的操作系统提供参考。
|
2月前
|
人工智能 vr&ar Android开发
探索安卓与iOS系统的发展趋势
【2月更文挑战第9天】 过去,人们对于安卓和iOS系统的争论主要集中在性能、用户体验和生态系统的比较上。然而,随着移动互联网的快速发展,两大操作系统在人工智能、物联网、安全性等方面的发展趋势也备受关注。本文将探讨安卓与iOS系统在技术发展方面的差异以及未来的发展趋势。