android lcd横竖屏幕配置

横屏

按照下面代码示例修改Activity的onResume方法

 

@Override
protected void onResume() {
 /**
  * 设置为横屏
  */
 if(getRequestedOrientation()!=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
 }
 super.onResume();
}

 

或者在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)

 

android:launchMode="singleTask" android:screenOrientation="portrait"> 

 

要设置成竖屏设置成 SCREEN_ORIENTATION_PORTRAIT
-------------------------------------------------------------------------------------
设定为竖屏,并禁止横竖屏切换:

在AndroidManifest.xml中,给每个Activity加上:
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"

(landscape是横向,portrait是纵向)
-------------------------------------------------------
关闭重力感应
android:screenOrientation="nosensor" 
横向
android:screenOrientation="landscape"
-------------------------------------------------------
android:screenOrientation的说明

在Android的官方文档当中,对android:screenOrientation的说明如下:

 

android:screenOrientation The orientation of the activity's display on the device.

The value can be any one of the following strings:

"unspecified" The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
"user" The user's current preferred orientation.
"behind" The same orientation as the activity that's immediately beneath it in the activity stack.
"landscape" Landscape orientation (the display is wider than it is tall).
"portrait" Portrait orientation (the display is taller than it is wide).
"reverseLandscape" Landscape orientation in the opposite direction from normal landscape.Added in API level 9.
"reversePortrait" Portrait orientation in the opposite direction from normal portrait.Added in API level 9.
"sensorLandscape" Landscape orientation, but can be either normal or reverse landscape based on the device sensor. Added in API level 9.
"sensorPortrait" Portrait orientation, but can be either normal or reverse portrait based on the device sensor. Added in API level 9.
"sensor" The orientation is determined by the device orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device. Some devices, though, will not rotate to all four possible orientations, by default. To allow all four orientations, use "fullSensor".
"fullSensor" The orientation is determined by the device orientation sensor for any of the 4 orientations. This is similar to "sensor" except this allows any of the 4 possible screen orientations, regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those). Added in API level 9.
"nosensor" The orientation is determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.

Note: When you declare one of the landscape or portrait values, it is considered a hard requirement for the orientation in which the activity runs. As such, the value you declare enables filtering by services such as Google Play so your application is available only to devices that support the orientation required by your activities. For example, if you declare either "landscape","reverseLandscape", or "sensorLandscape", then your application will be available only to devices that support landscape orientation. However, you should also explicitly declare that your application requires either portrait or landscape orientation with the <uses-feature> element. For example, <uses-feature android:name="android.hardware.screen.portrait"/>. This is purely a filtering behavior provided by Google Play (and other services that support it) and the platform itself does not control whether your app can be installed when a device supports only certain orientations.

译文为:

 

android:screenOrientation

Activity在屏幕当中显示的方向。属性值可以是下表中列出的一个值:

 

"unspecified" 默认值,由系统来选择方向。它的使用策略,以及由于选择时特定的上下文环境,可能会因为设备的差异而不同。
"user" 使用用户当前首选的方向。
"behind" 使用Activity堆栈中与该Activity之下的那个Activity的相同的方向。
"landscape" 横向显示(宽度比高度要大)
"portrait" 纵向显示(高度比宽度要大)
"reverseLandscape" 与正常的横向方向相反显示,在API Level 9中被引入。
"reversePortrait" 与正常的纵向方向相反显示,在API Level 9中被引入。
"sensorLandscape" 横向显示,但是基于设备传感器,既可以是按正常方向显示,也可以反向显示,在API Level 9中被引入。
"sensorPortrait" 纵向显示,但是基于设备传感器,既可以是按正常方向显示,也可以反向显示,在API Level 9中被引入。
"sensor" 显示的方向是由设备的方向传感器来决定的。显示方向依赖与用户怎样持有设备;当用户旋转设备时,显示的方向会改变。但是,默认情况下,有些设备不会在所有的四个方向上都旋转,因此要允许在所有的四个方向上都能旋转,就要使用fullSensor属性值。
"fullSensor" 显示的方向(4个方向)是由设备的方向传感器来决定的,除了它允许屏幕有4个显示方向之外,其他与设置为“sensor”时情况类似,不管什么样的设备,通常都会这么做。例如,某些设备通常不使用纵向倒转或横向反转,但是使用这个设置,还是会发生这样的反转。这个值在API Level 9中引入。
"nosensor" 屏幕的显示方向不会参照物理方向传感器。传感器会被忽略,所以显示不会因用户移动设备而旋转。除了这个差别之外,系统会使用与“unspecified”设置相同的策略来旋转屏幕的方向。

 

 

注意:在给这个属性设置的值是“landscape”或portrait的时候,要考虑硬件对Activity运行的方向要求。正因如此,这些声明的值能够被诸如Google Play这样的服务所过滤,以便应用程序只能适用于那些支持Activity所要求的方向的设备。例如,如果声明了“landscape”、“reverseLandscape”、或“sensorLandscape”,那么应用程序就只能适用于那些支持横向显示的设备。但是,还应该使用<uses-feature>元素来明确的声明应用程序所有的屏幕方向是纵向的还是横行的。例如:<uses-feature android:name=”android.hardware.screen.portrait”/>。这个设置由Google Play提供的纯粹的过滤行为,并且在设备仅支持某个特定的方向时,平台本身并不控制应用程序是否能够被按照。
---------------------------------------------------------------
Android Activity始终横屏、全屏、屏幕方向改变等屏幕相关           

博客分类:
 

android手机屏幕方向改变的时可以使应用程序随之相应改变,这里就简单介绍屏幕操作中的一点小技巧。包括始终横(竖)屏、全屏、屏幕方向改变时销毁Activity的问题。

 

1、屏幕始终横屏或竖屏

对于一些游戏,可能我们希望屏幕始终横屏,这时只需要在AndroidManifest.xml中对Activity属性进行设置

android:screenOrientation="landscape"表示始终横屏,

android:screenOrientation="portrait"表示始终竖屏,下面是始终横屏的例子

Java代码 复制代码 收藏代码
  1. <activity android:name="ViewStatusDetailActivity"  
  2.     android:screenOrientation="landscape"  
  3.         android:label="@string/app_name">  
  4.         </activity>  


android:screenOrientation的其他值见screenOrientation

 

2、Activity全屏

两种配置方式,

一种是在AndroidManifest.xml中对Activity属性进行设置,如下:

Java代码 复制代码 收藏代码
  1. <activity android:name="ViewStatusDetailActivity"  
  2.     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  3.     android:label="@string/app_name">  
  4.     </activity>  


 android:theme="@android:style/Theme.NoTitleBar.Fullscreen"表示全屏该Activity

 

第二种是在代码中进行设置

Java代码 复制代码 收藏代码
  1. public void onCreate(Bundle savedInstanceState) {   
  2.     super.onCreate(savedInstanceState);   
  3.     //无title     
  4.     requestWindowFeature(Window.FEATURE_NO_TITLE);     
  5.     //全屏     
  6.     getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,       
  7.         WindowManager.LayoutParams. FLAG_FULLSCREEN);            
  8.     setContentView(R.layout.main);   
  9.     }   
  10. }   


 其中requestWindowFeature和getWindow().setFlags必须一起使用,并且在setContentView前面

 

 

3、解决屏幕方向改变Activity销毁重建问题

当屏幕方向改变时,经常发现刚输入的文字被清空了、imageView图片不存在了,或是网络数据重新获取,其实是Activity会被销毁,重新调用OnCreate构建,如何防止这种情况呢,分为两步:

3.1 在AndroidManifest.xml中对Activity属性进行设置,如下:

Xml代码 复制代码 收藏代码
  1. <activity android:name="ViewStatusActivity"  
  2.                   android:configChanges="orientation|keyboardHidden"  
  3.                   android:label="@string/app_name"  
  4.                   android:theme ="@style/update_status_style">  
  5.         </activity>  


android:configChanges="orientation|keyboardHidden"表示改变界面方向和隐藏键盘

具体android:configChanges见configChanges

 

 

3.2 重载onConfigurationChanged方法,此方法会在屏幕方向改变时被调用如下:

Java代码 复制代码 收藏代码
  1. @Override  
  2. public void onConfigurationChanged(Configuration newConfig) {  
  3.   
  4.     super.onConfigurationChanged(newConfig);  
  5.   
  6.     if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {  
  7.         // 加入横屏要处理的代码  
  8.     } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {  
  9.         // 加入竖屏要处理的代码  
  10.     }  
  11. }  


------------------------------------------

Android系统Surface机制的SurfaceFlinger服务的启动过程分析
http://blog.csdn.net/luoshengyang/article/details/8046659 


--------------------------------------------
android竖屏显示             2010-04-14 14:25:15          

分类: 嵌入式

 

本文原创,来自http://blog.csdn.net/knock,转载请保留本行
 
屏是LANDSCAPE的,要让它默认显示为PORTRAIT.
 
1.kernel里要旋转FrameBuffer.
  启动参数里加入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会传递给其他需要的结构。
注意:参考$(kernel)/documentation/fb/fbcon.txt
 
2.android OS旋转屏幕
系统默认是针对竖屏的,而MID使用的是横屏,所以需要做一个转换的动作。
PORTRAIT               LANDSCAPE         <------屏幕显示方式
ROTATION_0             ROTATION_90
ROTATION_90        ROTATION_180
ROTATION_180        ROTATION_270
ROTATION_270        ROTATION_0
 
而source code里对ROTATION_180和ROTATION_270的处理比较少,只在sensor和KeyQueue部分,所以如果只是要让系统显示为竖屏,将android中的Surface.ROTATION_0改为Surface.ROTATION_90,而Surface.ROTATION_90改为Surface.ROTATION_0。 这样,启动后的屏幕就是竖屏的了。
改动后,启动时还是LANDSCAPE显示的,进入HOME也是,很快就会自动旋转到PORTRAIT模式,这是由于
$(cupcake)/frameworks/base/services/java/com/android/server/WindowManagerService.java
中enableScreenAfterBoot()->performEnableScreen()->mPolicy.enableScreenAfterBoot(), mPolicy为父类指针,可以指向
PhoneWindowManager或者MidWindowManager,由配置文件$(cupcake)/build/target/product/core.mk中
PRODUCT_POLICY := android.policy_phone
//PRODUCT_POLICY := android.policy_mid
来指定。
PhoneWindowManager::enableScreenAfterBoot()->updateRotation(Surface.FLAGS_ORIENTATION_ANIMATION_DISABLE)->mWindowManager.setRotation()完成设置旋转并清除LOGO.
 
3.启动过程中竖屏
启动过程中,默认是按照屏的width和height显示的,不会旋转,要使它显示logo时就是竖屏的,也就是旋转90度,需要做如下工作:
$(cupcake)/frameworks/base/libs/surfaceflinger/SurfaceFlinger.cpp
status_t SurfaceFlinger::readyToRun()中
    //const uint32_t w = hw.getWidth();
    //const uint32_t h = hw.getHeight();
//swap w&h for portrait display in landscape panel. jeff.
    const uint32_t h = hw.getWidth();  
    const uint32_t w = hw.getHeight();
交换一下width和height,这样后面用OpenGL创建的ViewPort形状就是竖的了。修改后面的函数参数也可以,不过太多了,交换一下省事。但是怎么让这个竖的viewport旋转90度呢?这里就要用到GraphicPlane::mGlobalTransform这个Transform了。它指示当前最终要旋转的结果。 所以要在创建GraphicPlane时初始化mGlobalTransform为旋转90度。
GraphicPlane::GraphicPlane()
    : mHw(0)
{
//add by jeff. for default rotate angel 90 
 mOrientationTransform.reset();
 mOrientation = ISurfaceComposer::eOrientation90;
 mGlobalTransform = mOrientationTransform * mTransform; 
}
此段从status_t GraphicPlane::setOrientation(int orientation)复制过来,注意修改mGlobalTransform:
    if (orientation == ISurfaceComposer::eOrientation90) { //ISurfaceComposer::eOrientationDefault //jeff
        // make sure the default orientation is optimal
        mOrientationTransform.reset();
        mOrientation = orientation;
        //mGlobalTransform = mTransform;
        mGlobalTransform = mOrientationTransform * mTransform; //jeff
        return NO_ERROR;
    }
注意mOrientationTransform.reset();要修改为默认旋转90度。参照status_t GraphicPlane::orientationToTransfrom
中的设置,修改为:
void Transform::reset() { 
    mTransform.reset();
    mType = 0;
 set(0,-1,1,0);  //jeff
 set(800,0);
}
参考:
status_t GraphicPlane::orientationToTransfrom(
        int orientation, int w, int h, Transform* tr)
{    
    float a, b, c, d, x, y;
    switch (orientation) {
    case ISurfaceComposer::eOrientationDefault:
        a=1; b=0; c=0; d=1; x=0; y=0;
        break;
    case ISurfaceComposer::eOrientation90:
        a=0; b=-1; c=1; d=0; x=w; y=0;
        break;
    case ISurfaceComposer::eOrientation180:
        a=-1; b=0; c=0; d=-1; x=w; y=h;
        break;
    case ISurfaceComposer::eOrientation270:
        a=0; b=1; c=-1; d=0; x=0; y=h;
        break;
    default:
        return BAD_VALUE;
    }
    tr->set(a, b, c, d);
    tr->set(x, y);
    return NO_ERROR;
}
修改之后,默认就是竖屏(旋转90度)显示了。
posted @ 2014-07-14 17:00  xiaoaidelala  阅读(905)  评论(0编辑  收藏  举报