Android屏幕重力感应旋转
2018-01-30 16:30
543 查看
public class MainActivity extends AppCompatActivity { private MyOrientoinListener myOrientoinListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myOrientoinListener = new MyOrientoinListener(this); boolean autoRotateOn = (android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1); //检查系统是否开启自动旋转 if (autoRotateOn) { myOrientoinListener.enable(); } } @Override protected void onDestroy() { super.onDestroy(); //销毁时取消监听 myOrientoinListener.disable(); } class MyOrientoinListener extends OrientationEventListener { public MyOrientoinListener(Context context) { super(context); } public MyOrientoinListener(Context context, int rate) { super(context, rate); } @Override public void onOrientationChanged(int orientation) { int screenOrientation = getResources().getConfiguration().orientation; if (((orientation >= 0) && (orientation < 45)) || (orientation > 315)) { //设置竖屏 if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } else if (orientation > 225 && orientation < 315) { //设置横屏 if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else if (orientation > 45 && orientation < 135) {// 设置反向横屏 if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } else if (orientation > 135 && orientation < 225) { //反向竖屏 if (screenOrientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } } } }
相关文章推荐
- android重力感应和屏幕旋转关系
- 让android程序根据重力感应旋转屏幕(支持4个方向旋转)
- Android 重力感应和屏幕旋转关系
- 让android程序根据重力感应旋转屏幕(支持4个方向旋转)
- Android 重力感应和屏幕旋转关系
- cocos2dx lua android 禁止屏幕旋转 重力感应
- android 重力感应和屏幕旋转关系
- android 重力感应和屏幕旋转关系
- Android 屏幕重力感应
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- Android屏幕旋转时Activity的生命周期
- Android屏幕旋转不重新加载布局
- Android 屏幕旋转相关解析
- Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案
- android 屏幕旋转
- IOS&Android通用屏幕控制(移动旋转缩放)
- Android开发实践:屏幕旋转的处理
- android屏幕旋转在framework中的修改。
- android屏幕旋转显示流程