閱讀952 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android如何改變係統默認橫豎屏方向

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

如何改變android默認的橫豎屏,修改源碼一個地方就可以了。


[java] view plaincopy
  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 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記錄 —— 屏幕改為豎屏


https://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;
                }


讓所有應用都橫屏顯示

https://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中的修改

https://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就這麼簡單的一修改,就可以全程豎屏顯示了!


最後更新:2017-04-03 18:51:56

  上一篇:go 小米的“文化麻醉”難長久
  下一篇:go 硬實時操作係統-RTLinux