android 屏幕旋转(横屏、竖屏、反向横屏、反向竖屏)的实现
前言:根据屏幕的旋转自动实现应用界面旋转,横屏、竖屏、反向横屏、反向竖屏。
布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_ori"
android:text="方向"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:src="@mipmap/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
监听器类:
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;
}
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
float[] values = sensorEvent.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();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int i) {
}
}
Activity类
public class OrientationActivity extends AppCompatActivity {
private Button button;
private Handler handler;
private OrientationSensorListener listener;
private SensorManager sm;
private Sensor sensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_orientation);
Log.i("mylog:", "onCreate()");
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 888) {
int orientation = msg.arg1;
if (orientation > 45 && orientation < 135) {
OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
Log.d("mylog", "横屏翻转: ");
} else if (orientation > 135 && orientation < 225) {
OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
Log.d("mylog", "竖屏翻转: ");
} else if (orientation > 225 && orientation < 315) {
OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d("mylog", "横屏: ");
} else if ((orientation > 315 && orientation < 360) || (orientation > 0 && orientation < 45)) {
OrientationActivity.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Log.d("mylog", "竖屏: ");
}
}
super.handleMessage(msg);
}
};
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
listener = new OrientationSensorListener(handler);
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
button = (Button) findViewById(R.id.btn_ori);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("mylog:", getResources().getConfiguration().orientation +"");
}
});
}
@Override
protected void onPause() {
super.onPause();
sm.unregisterListener(listener);
}
@Override
protected void onResume() {
super.onResume();
sm.registerListener(listener, sensor, SensorManager.SENSOR_DELAY_UI);
}
}
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ndt.testdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".com.OrientationActivity"
android:configChanges="keyboardHidden|screenSize|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>