开发过程中遇到此效果,在此记录,手机默认状态下只只支持这三个方向的旋转,而平板支持4个方向可以旋转。现在实现平板和手机一样只支持三个方向。代码如下:
1.自定义监听extends OrientationEventListener
package com.yhy.myapplication22;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.OrientationEventListener;
/**
* Created by ${yinhaiyang} on 2018/4/3.
* android的屏幕旋转只能一次旋转90度,如果你突然一下子旋转180度,
* onConfigurationChanged函数不会被调用。
* 所以此时可以通过调用这个监听方法实现
*/
public class MyOrientationDetector extends OrientationEventListener {
MyOrientationDetector detector;
Activity mContext;
int rate;
public MyOrientationDetector(Context context) {
super(context);
this.mContext = (Activity) context;
}
public MyOrientationDetector(Context context, int rate) {
super(context, rate);
this.mContext = (Activity) context;
this.rate = rate;
}
@Override
public void onOrientationChanged(int orientation) {
Log.i("MyOrientationDetector ", "onOrientationChanged:" + orientation);
final int rotation = mContext.getWindowManager().getDefaultDisplay().getOrientation();
if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
return; //手机平放时,检测不到有效的角度
}
//只检测是否有四个角度的改变
if (orientation > 350 || orientation < 10) { //0度
orientation = 0;
} else if (orientation > 80 && orientation < 100) { //90度
orientation = 90;
} else if (orientation > 170 && orientation < 190) { //180度
orientation = 180;
} else if (orientation > 260 && orientation < 280) { //270度
orientation = 270;
} else {
return;
}
if (orientation == 0)
{
mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
} else if (orientation == 90)
{
mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);//横屏
} else if (orientation == 180)
{
mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
} else if (orientation == 270)
{
mContext.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);//横屏
}
}
}
2.在Activity中调用
myOrientationDetector=new MyOrientationDetector(this,SensorManager.SENSOR_DELAY_NORMAL);//在oncreate中调用。
@Override
protected void onResume() {
super.onResume();
//开启屏幕旋转方向监听
myOrientationDetector.enable();
}
@Override
protected void onPause() {
super.onPause();
//关闭监听
myOrientationDetector.disable();
}
3.Manifest中配置如下代码:保证屏幕旋转的时候不销毁Activity
原因:缺省状态下,Activity每次横竖屏切换(包括用setRequestedOrientation调用)都会重新调用一轮onPause-> onStop-> onDestory-> onCreate->onStart->onResume操作,从而销毁原来的Activity对象,创建新的Activity对象,这是因为通常情况下软件在横竖屏之间切换,界面的高宽会发生转换,从而可能会要求有不同的布局。因此我们加入如下设置,可以有效避免横竖屏切换的时候,activity的销毁重建。
android:configChanges="keyboardHidden|orientation|screensize"
<activity android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
4.manifest中不设置configchages属性,让acticity 重建,通过onSaveInstanceState()来保存,activity销毁前的状态和数据。
说明:当屏幕旋转时,这个Configuration就发生了改变,因此当前显示的Activity需要被重建,Activity对象会被终止,它的onPause()、onStop()和onDestroy()方法依次触发,然后一个新的Activity对象被创建,onCreate()方法被触发。假设屏幕旋转前,用户正在手机上填写一个注册表单,如果处理不当,用户会发现旋转后的表单变成空白的了,严重影响使用体验。
//旋转后,恢复数据
----------
package com.example.scareenchange;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editText1;
private int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=(EditText) findViewById(R.id.editText1);
//注意一定要加if语句,不然程序异常
if(savedInstanceState!=null) {
//通过Bundle对象取出
int i=savedInstanceState.getInt("info");
}
}
public void button1(View v) {
editText1.setText((i++)+"");
}
/*
*这个方法会在重新创建Activity之前调用
*我们在这个方法里保存对象,以解决Activity重新创建问题
*
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
Toast.makeText(getApplicationContext(), i+"", 1000).show();
outState.putInt("info", i);
}
}
注:方法3、4要达到的目的一样,任选其一,推荐第4种。
demo参考地址:点击打开链接https://download.csdn.net/download/yhy123456q/10325174