`
GhostFromheaven
  • 浏览: 393994 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Android控制屏幕方向的改变

阅读更多

     目前大多数手机都支持 重力感应 ,随之而来的就是屏幕方向改变的问题。很多游戏都是仅横屏展示的,也有一些是仅竖屏展示的,更多的是横屏竖屏都可以的。

 

    对应普通开发者来说,屏幕的随意改变也会带来困扰。在Google自带的doc里可以看到

  如果设备的配置(在 Resources.Configuration 中进行了定义)发生改变,那么所有用户界面上的东西都需要进行更新,以适应新的配置。因为 Activity 是与用户交互的最主要的机制,它包含了处理配置改变的专门支持。除非你特殊指定,否则当配置发生改变(比如屏幕方向、语言、输入设备等等的改变)时你当前的 activity 都将被销毁,这销毁是通过一个正常的 activity 生命周期过程( onFreeze(Bundle) , onPause() , onStop() , onDestroy() )进行的。如果 activity 之前正在前景画面,当这个实例的 onDestroy() 调用完成后将会启动这个 activity 的一个新的实例,并将前面那个实例中 onFreeze(Bundle) 所保存的内容传递给新的实例。因为任何的应用资源(包括 layout 文件)都有可能由于任何配置值而改变。因此处理配置改变的唯一安全的方法就是重新获取所有的资源,包括 layout 、绘图资源(原文 drawables )、字符串资源。由于 activity 已经如何保存自己的状态并从这些状态中重建自身,所以 activity 重新启动自身来获得新的配置将是一个非常便利的途径。在一些特殊的情况中,你可能希望当一种或者多种配置改变时避免重新启动你的 activity 。你可以通过在manifest中设置 android:configChanges 属性来实现这点。你可以在这里声明 activity 可以处理的任何配置改变,当这些配置改变时不会重新启动 activity ,而会调用 activity onConfigurationChanged(Resources.Configuration) 方法。如果改变的配置中包含了你所无法处理的配置(在 android:configChanges 并未声明),你的 activity 仍然要被重新启动,而 onConfigurationChanged(Resources.Configuration) 将不会被调用。

 

   在屏幕方向改变的时候,如果没有处理,程序会自动重启。对应一些需要保存用户数据的应用中,必须处理这种情况。

 

1>在AndroidManifest.xml中设置Activity的android:configChanges 属性。如:

 

<activity android:name=".AndroidLight"
                  android:label="@string/app_name"
                  android:configChanges="orientation|keyboardHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

 

 

 

  

这样就指定了屏幕方向改变和键盘隐藏时通知程序。

 

2>在程序中可以添加处理事件

 

@Override
	public void onConfigurationChanged(Configuration newConfig) {
	  super.onConfigurationChanged(newConfig);
	  Log.d(TAG," == onConfigurationChanged");
	  processLayout();//自定义函数处理配置改变事件
	}

 

 

3>也可以在AndroidManifest.xml中设置Activity的android:screenOrientation属性。如

 

<activity android:name=".AndroidLight"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

 

  

这样指定屏幕方向为竖屏。屏幕就不会自动旋转了。

 

 

4> android:configChanges 可选属性值 

 

 

Constant

Value

Description

mcc

0x0001

The IMSI MCC has changed, that is a SIM has been detected and updated the Mobile Country Code.

mnc

0x0002

The IMSI MNC has changed, that is a SIM has been detected and updated the Mobile Network Code.

locale

0x0004

The locale has changed, that is the user has selected a new language that text should be displayed in.

touchscreen

0x0008

The touchscreen has changed. Should never normally happen.

keyboard

0x0010

The keyboard type has changed, for example the user has plugged in an external keyboard.

keyboardHidden

0x0020

The keyboard or navigation accessibility has changed, for example the user has slid the keyboard out to expose it. Note that despite its name, this applied to any accessibility: keyboard or navigation.

navigation

0x0040

The navigation type has changed. Should never normally happen.

orientation

0x0080

The screen orientation has changed, that is the user has rotated the device.

screenLayout

0x0100

The screen layout has changed. This might be caused by a different display being activated.

uiMode

0x0200

The global user interface mode has changed. For example, going in or out of car mode, night mode changing, etc.

fontScale

0x40000000

The font scaling factor has changed, that is the user has selected a new global font size.

 

 

 

5>android:screenOrientation的可选属性值

Constant

Value

Description

unspecified

-1

No preference specified: let the system decide the best orientation. This will either be the orientation selected by the activity below, or the user's preferred orientation if this activity is the bottom of a task. If the user explicitly turned off sensor based orientation through settings sensor based device rotation will be ignored. If not by default sensor based orientation will be taken into account and the orientation will changed based on how the user rotates the device

landscape

0

Would like to have the screen in a landscape orientation: that is, with the display wider than it is tall, ignoring sensor data.

portrait

1

Would like to have the screen in a portrait orientation: that is, with the display taller than it is wide, ignoring sensor data.

user

2

Use the user's current preferred orientation of the handset.

behind

3

Keep the screen in the same orientation as whatever is behind this activity.

sensor

4

Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device.

nosensor

5

Always ignore orientation determined by orientation sensor: the display will not rotate when the user moves the device.

sensorLandscape

6

Would like to have the screen in landscape orientation, but can use the sensor to change which direction the screen is facing.

sensorPortait

7

Would like to have the screen in portrait orientation, but can use the sensor to change which direction the screen is facing.

reverseLandscape

8

Would like to have the screen in landscape orientation, turned in the opposite direction from normal landscape.

reversePortait

9

Would like to have the screen in portrait orientation, turned in the opposite direction from normal portrait.

fullSensor

10

Orientation is determined by a physical orientation sensor: the display will rotate based on how the user moves the device. This allows any of the 4 possible rotations, regardless of what the device will normally do (for example some devices won't normally use 180 degree rotation).

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论
1 楼 sunnyting 2012-06-15  
蛮靠谱!解决问题哈!

相关推荐

    Android 屏幕旋转(改变屏幕方向).rar

    Android 屏幕旋转实例,改变屏幕方向,这个和平时的锁定屏幕方向有关联,其基本的实现思路如下:  public void onConfigurationChanged(Configuration newConfig) {   Toast.makeText(this, "系统的屏幕方向发生...

    android改变变屏幕方向.docx

    android改变变屏幕方向.docx

    android Activity始终横屏、全屏、屏幕方向改变等屏幕相关.doc

    android Activity始终横屏、全屏、屏幕方向改变等屏幕相关

    强制改变屏幕方向的小工程

    Android强制横竖屏切换的一个工具代码,灰常的简单实用~

    Android改变手机屏幕朝向的方法

    主要介绍了Android改变手机屏幕朝向的方法,涉及Android动态更改手机屏幕朝向的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

    迅为4412开发板-Android系统屏幕旋转设置

    客户需要根据自己的产品设计及应用环境,切换屏幕的显示方向,或者固定好一个显示方向,例如产品中使用不同分辨率的显示屏,或者显示屏在产品中的固定方向发生改变等等,都需要进行屏幕旋转功能。 那么如何设置屏幕的...

    Android屏幕大小相关技巧应用指南.docx

    在这里我们就可以为大家详细介绍一下有关Android屏幕大小的自适应方式,帮助大家理解。不同的Androidtarget会有不同的大小,应用程序的界面需要针对不同的大小调整界面元素的尺寸。而且Android屏幕大小也可以在横屏...

    android改变方向

    这个app展示了在屏幕方向改变的时候app经历的步骤。

    Android开发 旋转屏幕导致Activity重建解决方法

    “屏幕方向”(orientation)是一个Configuration,通过查看Configuration类的javadoc可以看到其他Configuration还有哪些:如fontScale、keyboardHidden和locale等等。 当屏幕旋转时,这个Configuration就发生了...

    新版Android开发教程.rar

    � A ndroid 在设计初期就考虑了与现其有业务的融合,改变以往从计算机为主改成从手机使用为导向。新 生应用如:G oogle 地图及其衍生应用、 GMail 、 GTalk 等。 � GPS 卫星导航功能,手机照相, MP3 ,蓝芽等均...

    android实现在横竖屏切换时页面信息不被重置的示例分享

    当屏幕转动切换的时候 Android 机制是:销毁当前屏幕的 Activity ,然后重新开启一个新的适应屏幕改变的 Activity 。那么,我们该如何在屏幕切换的...表示在改变屏幕方向、弹出软件盘和隐藏软键盘时,不再去执行 onCrea

    Maze 400 迷宫 android小游戏项目源码 JavaScript Java

    在屏幕上滑动以改变方向并引导点穿过迷宫。 MAZE 400:你在这个流行的游戏的任务很简单:找到出口,逃离迷宫!在屏幕上滑动以改变方向并引导点穿过迷宫。从三种模式中选择一种适合您的偏好: * 经典模式与越来越困难...

    Android天气预报应用

    (4)在用户改变手机方向导致屏幕在横屏和竖屏之间切换时,能够根据实际情况对界面布局进行调整。 (5)如果不能查询到相应城市的天气信息,则需要进行异常判断和处理 (6)提供更换皮肤的功能(让用户在内置皮肤或...

    android开发秘籍

    2.2.3 秘诀4:强制屏幕方向 26 2.2.4 秘诀5:保存和恢复activity信息 27 2.3 多个activity 28 2.3.1 秘诀6:使用按钮和文本框 28 2.3.2 秘诀7:通过事件启动另外一个activity 29 2.3.3 秘诀8:将语音转换成文本并...

    Android开发应用实战详解源代码

    5.18 变换屏幕的方向 5.19 获取设备信息 5.20 小结 第6章 手机自动服务 6.1 实现短信提醒 6.2 剩余电量提醒 6.3 短信群发 6.4 短信e-mail通知 6.5 来电提醒 6.5.1 实现原理 6.5.2 telephonymanager和...

    Google Android SDK开发范例大全(PDF高清完整版1)(4-1)

    5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...

    Google Android SDK开发范例大全(PDF完整版4)(4-4)

    5.22 动态更改屏幕方向——LANDSCAPE与PORTRAIT 5.23 系统设置更改事件——onConfigurationChanged信息处理 5.24 取得电信网络与手机相关信息——TelephonyManager与android.provider.Settings.System的应用 第6章 ...

Global site tag (gtag.js) - Google Analytics