分享

android 屏幕旋转问题

 苏霈儿咪咪 2013-09-23
要让程序界面保持一个方向,不随手机方向转动而变化的处理办法:
       在AndroidManifest.xml里面配置一下就可以了。加入这一行android:screenOrientation="landscape"。 例如(landscape是横向,portrait是纵向):
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas./apk/res/android" 
package="eoe.demo" 
android:versionCode="1" 
android:versionName="1.0"> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".Main" 
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> 
<activity android:name=".GamePlay" 
android:screenOrientation="portrait"></activity> 
<activity android:name=".OptionView" 
android:screenOrientation="portrait"></activity> 
</application> 
<uses-sdk android:minSdkVersion="3" /> 
</manifest> 

  有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入 android:screenOrientation=”landscape” 属性。
      landscape = 横向
       portrait = 纵向

      避免在转屏时重启Activity
       android中每次屏幕方向切换时都会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次 Create的时候载入配置,那样,进行中的游戏就不会自动重启了!

       要避免在转屏时重启Activity,可以通过在AndroidManifest.xml文件中重新定义方向(给每个Activity加上 android:configChanges=”keyboardHidden|orientation”属性)。

       在需要控制屏幕显示方向的Activity中重写 onConfigurationChanged(Configuration newConfig)方法,这样在转屏时就不会重启Activity了。

java代码:

if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
//横向
setContentView(R.layout.file_list_landscape);
}else{
//竖向
setContentView(R.layout.file_list);
}

settings设置
AndroidSettings-> Display中有Orientation这一设置项。当选中时,屏幕会随设备旋转
settings设置是在文件SoundAndDisplaySettings.java中,该项对应的键字符串为:

private
static
finalString KEY_ACCELEROMETER ="accelerometer";

其默认值保存在xml文件中,默认是EnableUI程序初始化时会根据其值是否在复选框中打勾(代码在onCreate函数中):

1.
protected
voidonCreate(Bundle savedInstanceState) {  
2.…  
3.        mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);  
4.        mAccelerometer.setPersistent(false);  
5.…  
6.}  

当用户改变了该值时,会保存起来:

1.public
booleanonPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {  
2.…  
3.    }else
if(preference == mAccelerometer) {  
4.            Settings.System.putInt(getContentResolver(),  
5.                    Settings.System.ACCELEROMETER_ROTATION,  
6.                    mAccelerometer.isChecked() ?1:0);  
7.…  
8.        }  
frameworks/policies/base/phone/com/android/internal/policy/impl/PhoneWindowManager.java中的SettingsServer会随时监控其值,对用户设置做出反应:


1.public
voidupdate() {  
2.            ContentResolver resolver = mContext.getContentResolver();  
3.
booleanupdateRotation =false;  
4.
synchronized(mLock) {  
5.                …  
6.
intaccelerometerDefault = Settings.System.getInt(resolver,  
7.                        Settings.System.ACCELEROMETER_ROTATION, DEFAULT_ACCELEROMETER_ROTATION);  
8.
if(mAccelerometerDefault != accelerometerDefault) {  
9.                    mAccelerometerDefault = accelerometerDefault;  
10.                    updateOrientationListenerLp();  
11.                }  
12.…  
13.}  
   G-sensor屏幕旋转

WindowOrientationListener
frameworks/base/core/java/android/view/WindowOrientationListener.java中会监听Sensor的值,对旋转方向进行判断,然后调用抽象方法onOrientationChanged,因此,只要在子类Listener中重新实现这个函数即可对四个不同方向做出响应 。

    PhoneWindowManager
frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java有个Listener
MyOrientationListener
mOrientationListener
MyOrientationListener  继承了WindowOrientationListener类
它会根据Sensor判别出的旋转方向,调用WindowManagerService::setRotation让屏幕进行旋转。
        class MyOrientationListener extends WindowOrientationListener {               MyOrientationListener(Context context) {                super(context);           }
        @Override         public void onOrientationChanged(int rotation) {            // Send updates based on orientation value
if (localLOGV) Log.v(TAG, "onOrientationChanged, rotation changed to " +rotation);             try {
mWindowManager.setRotation(rotation, false,                        mFancyRotationAnimation);             } catch (RemoteException e) {                // Ignore             }           }                                              }
MyOrientationListener mOrientationListener;

当应用程序显示禁止屏幕旋转时则不会旋转,见函数PhoneWindowManager ::needSensorRunningLp()。
 
 
最后通过代码来实现自动屏幕旋转这个功能

 

先判断是否打开:

需要导入的包为:import android.provider.Settings;
在还得在manifest里面设置android:configChanges="orientation|keyboardHidden"
android:screenOrientation="user"
并且要加上权限:
android.permission.WRITE_SETTINGS

代码为:
int flag =Settings.System.getInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0);
打开关闭,关闭打开:
Settings.System.putInt(context.getContentResolver(),Settings.System.ACCELEROMETER_ROTATION,flag==1?0:1);


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多