1、功能类
package com.tongyi.edu.util;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Handler;
import android.os.Message;
public class ScreenSwitchUtils {
private volatile static ScreenSwitchUtils mInstance;
private Activity mActivity;
// 是否是竖屏
private boolean isPortrait = true;
private SensorManager sm;
private OrientationSensorListener listener;
private Sensor sensor;
private SensorManager sm1;
private Sensor sensor1;
private OrientationSensorListener1 listener1;
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 888:
int orientation = msg.arg1;
if (orientation > 45 && orientation < 135) {
if (isPortrait) {
//切换成横屏反向:ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
mActivity.setRequestedOrientation(8);
isPortrait = false;
}
} else if (orientation > 135 && orientation < 225) {
if (!isPortrait) {
/*
* 切换成竖屏反向:ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT(9),
* ActivityInfo.SCREEN_ORIENTATION_SENSOR:根据重力感应自动旋转
* 此处正常应该是上面第一个属性,但是在真机测试时显示为竖屏正向,所以用第二个替代
*/
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
isPortrait = true;
}
} else if (orientation > 225 && orientation < 315) {
if (isPortrait) {
//切换成横屏:ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
mActivity.setRequestedOrientation(0);
isPortrait = false;
}
} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
if (!isPortrait) {
//切换成竖屏ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
mActivity.setRequestedOrientation(1);
isPortrait = true;
}
}
break;
default:
break;
}
};
};
/** 返回ScreenSwitchUtils单例 **/
public static ScreenSwitchUtils init(Context context) {
if (mInstance == null) {
synchronized (ScreenSwitchUtils.class) {
if (mInstance == null) {
mInstance = new ScreenSwitchUtils(context);
}
}
}
return mInstance;
}
private ScreenSwitchUtils(Context context) {
// 注册重力感应器,监听屏幕旋转
sm = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new OrientationSensorListener(mHandler);
// 根据 旋转之后/点击全屏之后 两者方向一致,激活sm.
sm1 = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
sensor1 = sm1.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener1 = new OrientationSensorListener1();
}
/** 开始监听 */
public void start(Activity activity) {
mActivity = activity;
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
}
/** 停止监听 */
public void stop() {
sm.unregisterListener(listener);
sm1.unregisterListener(listener1);
}
/**
* 手动横竖屏切换方向
*/
public void toggleScreen() {
sm.unregisterListener(listener);
sm1.registerListener(listener1, sensor1,SensorManager.SENSOR_DELAY_UI);
if (isPortrait) {
isPortrait = false;
// 切换成横屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
isPortrait = true;
// 切换成竖屏
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
public boolean isPortrait(){
return this.isPortrait;
}
/**
* 重力感应监听者
*/
public class OrientationSensorListener implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
private Handler rotateHandler;
public OrientationSensorListener(Handler handler) {
rotateHandler = handler;
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int orientation = ORIENTATION_UNKNOWN;
float X = -values[_DATA_X];
float Y = -values[_DATA_Y];
float Z = -values[_DATA_Z];
float magnitude = X * X + Y * Y;
// Don't trust the angle if the magnitude is small compared to the y
// value
if (magnitude * 4 >= Z * Z) {
// 屏幕旋转时
float OneEightyOverPi = 57.29577957855f;
float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int) Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if (rotateHandler != null) {
rotateHandler.obtainMessage(888, orientation, 0).sendToTarget();
}
}
}
public class OrientationSensorListener1 implements SensorEventListener {
private static final int _DATA_X = 0;
private static final int _DATA_Y = 1;
private static final int _DATA_Z = 2;
public static final int ORIENTATION_UNKNOWN = -1;
public OrientationSensorListener1() {
}
public void onAccuracyChanged(Sensor arg0, int arg1) {
}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
int orientation = ORIENTATION_UNKNOWN;
float X = -values[_DATA_X];
float Y = -values[_DATA_Y];
float Z = -values[_DATA_Z];
float magnitude = X * X + Y * Y;
// Don't trust the angle if the magnitude is small compared to the y
// value
if (magnitude * 4 >= Z * Z) {
// 屏幕旋转时
float OneEightyOverPi = 57.29577957855f;
float angle = (float) Math.atan2(-Y, X) * OneEightyOverPi;
orientation = 90 - (int) Math.round(angle);
// normalize to 0 - 359 range
while (orientation >= 360) {
orientation -= 360;
}
while (orientation < 0) {
orientation += 360;
}
}
if (orientation > 225 && orientation < 315) {// 检测到当前实际是横屏
if (!isPortrait) {
sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);
sm1.unregisterListener(listener1);
}
} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {// 检测到当前实际是竖屏
if (isPortrait) {
sm.registerListener(listener, sensor,SensorManager.SENSOR_DELAY_UI);
sm1.unregisterListener(listener1);
}
}
}
}
}
2、使用:
public class MainActivity extends Activity implements OnClickListener {
private ScreenSwitchUtils instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instance = ScreenSwitchUtils.init(this.getApplicationContext());
}
@Override
protected void onStart() {
super.onStart();
instance.start(this);
}
@Override
protected void onStop() {
super.onStop();
instance.stop();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (instance.isPortrait()) {
// 切换成竖屏
} else {
// 切换成横屏
}
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.iv_stretch:
instance.toggleScreen();
break;
}
}
}
附:
"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”设置相同的策略来旋转屏幕的方向。 |