android如何改變係統默認橫豎屏方向
https://blog.csdn.net/abc19842008/article/details/7543559
如何改變android默認的橫豎屏,修改源碼一個地方就可以了。
- 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也不同。因為代碼開頭就有
- 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記錄 —— 屏幕改為豎屏
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; }
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