您的位置:首页 > 移动开发 > Android开发

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

2017-02-23 11:19 393 查看
http://blog.csdn.net/abc19842008/article/details/7543559

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

[java] view
plaincopy

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_90;  

               mLandscapeRotation = Surface.ROTATION_0;  

               mUpsideDownRotation = Surface.ROTATION_270;  

               mSeascapeRotation = Surface.ROTATION_180;  

           } else {  

               mPortraitRotation = Surface.ROTATION_0;  

               mLandscapeRotation = Surface.ROTATION_90;  

               mUpsideDownRotation = Surface.ROTATION_180;  

               mSeascapeRotation = Surface.ROTATION_270;  

           }  

       }  

  

    {  

        Log.i(TAG, "MediaPlayer.is not PlayingVideo");  

        synchronized (mLock) {  

            switch (orientation) {  

                case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:  

                    //always return portrait if orientation set to portrait  

                    //return mPortraitRotation;  

                    return mUpsideDownRotation;  

                case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:  

                    //always return landscape if orientation set to landscape  

                    return mLandscapeRotation;  

                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:  

                    //always return portrait if orientation set to portrait  

                    //return mUpsideDownRotation;  

                    return mPortraitRotation;  

                case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:  

                    //always return seascape if orientation set to reverse landscape  

                    return mSeascapeRotation;  

                case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:  

                    //return either landscape rotation based on the sensor  

                    mOrientationListener.setAllow180Rotation(  

                            isLandscapeOrSeascape(Surface.ROTATION_180));  

                    return getCurrentLandscapeRotation(lastRotation);  

                case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:  

                    mOrientationListener.setAllow180Rotation(  

                            !isLandscapeOrSeascape(Surface.ROTATION_180));  

                    return getCurrentPortraitRotation(lastRotation);  

            }  

  

            mOrientationListener.setAllow180Rotation(  

                   orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR  

                   || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);  

  

            // case for nosensor meaning ignore sensor and consider only lid  

            // or orientation sensor disabled  

            //or case.unspecified  

            if (mLidOpen) {  

                return mLidOpenRotation;  

            } else if (mDockMode == Intent.EXTRA_DOCK_STATE_CAR && mCarDockRotation >= 0) {  

                return mCarDockRotation;  

            } else if (mDockMode == Intent.EXTRA_DOCK_STATE_DESK && mDeskDockRotation >= 0) {  

                return mDeskDockRotation;  

            } else {  

                if (useSensorForOrientationLp(orientation)) {  

                    return mOrientationListener.getCurrentRotation(lastRotation);  

                }  

                return Surface.ROTATION_0;  

            }  

        }  

}  

   }  

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

[java] view
plaincopy

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_90;  

        mLandscapeRotation = Surface.ROTATION_0;  

        mUpsideDownRotation = Surface.ROTATION_270;  

        mSeascapeRotation = Surface.ROTATION_180;  

    } else {  

        mPortraitRotation = Surface.ROTATION_0;  

        mLandscapeRotation = Surface.ROTATION_90;  

        mUpsideDownRotation = Surface.ROTATION_180;  

        mSeascapeRotation = Surface.ROTATION_270;  

    }  

}  


调试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

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游戏, 修改点如下:

[java] view
plaincopy

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就这么简单的一修改,就可以全程竖屏显示了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: