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

时间: 2024-08-02 15:02:54

android如何改变系统默认横竖屏方向的相关文章

android如何获得系统默认相册路径及此路径下所有照片路径

问题描述 android如何获得系统默认相册路径及此路径下所有照片路径 我在做一个云相册的项目,用的是百度云.现在要获得系统默认相册的所有图片路径,在google上搜罗很多,都是把sd卡上所有相册都罗列了出来,我是安卓新手,也不太懂怎么弄,各位大神有什么好办法... 解决方案 系统默认相册路径一般都是/DCIM/camera 解决方案二: 遍历SD卡所有的文件夹以及文件夹下所有的文件,并通过文件的后缀判断文件是否属于图片文件(图片文件的后缀一般是.jpg,.png,.bmp等等),将文件的路径存

android 4.0以上版本横竖屏切换后不重建不销毁Activity

在AndroidManifest.xml文件的activity节点设置属性 android:configChanges="keyboardHidden|orientation"后可以使应用横竖屏切换时,不会重启对应的 Activity重新加载.可以在对应的Activity java类中重写onConfigurationChanged(Configuration newConfig)方法来处理纵横屏各自的布局,但要记得最后调用super.onConfigurationChanged(ne

Android Studio 导入 Vitamio及横竖屏切换

建议不要使用Vitamio,各种深坑 https://github.com/Bilibili/ijkplayer看看bilibili的开源播放器 Vitamio 官方:https://www.vitamio.org/ GitHub:https://github.com/yixia/VitamioBundle Vitamio 能够流畅播放720P甚至1080P高清MKV,FLV,MP4,MOV,TS,RMVB等常见格式的视频,支持 MMS, RTSP, RTMP, HLS(m3u8) 等常见的多种

关于Android横竖屏切换及Activity重启周期的总结

禁止Android横竖屏和解决切换屏幕时重启Activity的方法 1.在AndroidManifest.xml的Activity配置中加入 android:screenOrientation="landscape"属性(landscape是横向,portrait是纵向).如: <activity android:name=".ContactsManagerActivity" android:label="@string/app_name"

Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法_Android

本文实例讲述了Android编程实现横竖屏切换时不销毁当前activity和锁定屏幕的方法.分享给大家供大家参考,具体如下: 首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性 <activityandroid:name=".FileBrowser"android:label="@string/app_name"android:

横竖屏 切换-Android横竖屏切换有什么差别呢?

问题描述 Android横竖屏切换有什么差别呢? 个人感觉Android的横竖屏切换是因为重力的感应,之后软件的布局会改变, 但是横竖屏切换的具体差别是什么? 解决方案 页面:页面会重新加载,只要你的布局不是绝对.写死的,通常会自动适应,一般不会出现大问题. 数据:安卓内置的bundle会帮你保存一些系统认为比较重要的数据,还有一些数据是可能会丢失,需要你自己手动保存(具体查看onSaveInstance()等方法) 解决方案二: 页面会重新加载的,因为如果你不在Mainfest.xml文件中配

改变windows 7系统默认声音的方法

有没有尝试改变默认的Windows声音?如果没有,那么应该windows的所有版本都一个样.现在,由我来说说,在Windows 7操作系统如何改变系统默认声音.我安装系统的第一件事就是删除系统默认的声音方案,包括Windows 7或任何其他版本的Windows,因为我微软系统默认的系统声音总是一成不变,有点烦人. 如果你也有意改变系统声音,请参考下面的教程: 1. 右键单击空白桌面和选择个性化. 2. 选择窗口中的声音选项. 3.接着选择列表的其一程序事件 ,然后点击浏览.注意你自定义的声音文件

Android改变系统自带环形ProgressBar的大小

MainActivity如下: package cc.testprogressbar; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 改变系统自带环形ProgressBar的大小 * * 改变方式: * 为ProgressBar设置一个style即可 * 参见styles.xml * */ public class MainActivity extends Activity { @Override p

Android(2.2/2.3系统)Gallery解决默认和横竖屏切换选中状态问题_Android

前言  Gallery的Item使用的是一个ImageView+TextView,并且为其设置了selector,当使用setSelection设置时.横竖屏切换时Item的状态不会改变,这个目前在2.2/2.3系统中存在,高版本如4.0是不存在的. 正文  一.第一步,解决ImageView的状态问题   为ImageView设置  :android:focusableInTouchMode="true"   注意同样属性设置TextView不管用.  二.第二步,手动控制文本根据状