您的位置:首页 > 移动开发 > Android开发

android OrientationEventListener监测指定的屏幕旋转角度

2018-01-03 14:14 197 查看
关于OrientationEventListener监测指定的屏幕旋转角度,可以从自己的开发的场景不同进行使用;

不多说了,直接上代码;

public class MainActivity extends Activity {
OrientationEventListener mOrientationListener;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {

@Override
public void onOrientationChanged(int orientation) {
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 (mOrientationListener.canDetectOrientation()) {
mOrientationListener.enable();
} else {
mOrientationListener.disable();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}


Android屏幕旋转使用http://blog.csdn.net/xiao_yuanjl/article/details/78960427
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android