家资是何物,积帙列梁梠。这篇文章主要讲述android手机旋转方向识别相关的知识,希望能为你提供帮助。
private OrientationEventListener mOrientationListener; private String TAG = "MainActivity"; private int screenCurOrient = 2; //1表示正竖屏,2表示正横屏,3表示反竖屏,4表示反横屏private final void startOrientationChangeListener() { mOrientationListener = new OrientationEventListener(this) { @Override public void onOrientationChanged(int rotation) { //判断四个方向 if (rotation == -1) { Log.d(TAG, "手机平放:" + rotation); } else if (rotation < 10 || rotation > 350) { screenOrientChange(1); } else if (rotation < 100 & & rotation > 80) { screenOrientChange(4); } else if (rotation < 190 & & rotation > 170) { screenOrientChange(3); } else if (rotation < 280 & & rotation > 260) { screenOrientChange(2); } else { } } }; mOrientationListener.enable(); }private void screenOrientChange(int Orient) { if(Orient != screenCurOrient) { screenCurOrient = Orient; switch (screenCurOrient) { case 1: Log.d(TAG, "正竖屏:"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case 2: Log.d(TAG, "正横屏:"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; case 3: Log.d(TAG, "反竖屏:"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); break; case 4: Log.d(TAG, "反横屏:"); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); break; } } }
在onCreate方法中调用startOrientationChangeListener方法即可。
【android手机旋转方向识别】
推荐阅读
- mac 下 react Native android环境搭建
- webApp开发-angular1+ 功能模块划分
- Android API的一点了解
- A Multi-Sensorial Simultaneous Localization and Mapping (SLAM) System for Low-Cost Micro Aerial Vehi
- Yeoman 官网教学案例(使用 Yeoman 构建 WebApp)
- 初识android
- 如何修复Xbox错误代码0x800c0005(解决办法介绍)
- 网络测试技术的进展与动向
- 网络故障及技巧综合集锦