背景
开发中,有时需要获取当前屏幕的方向,所以单独写了个demo,方便大家参考。
Demo源代码:https://gitee.com/hspbc/screenOrientation.git
主要代码:public class MainActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private CameraOrientationListener orientationListener;
private TextView txt;
/**
* 当前屏幕旋转角度
*/
private int mOrientation = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = findViewById(R.id.textView);
orientationListener = new CameraOrientationListener(this);
orientationListener.enable();
startOrientationChangeListener();
}
/**
* 启动屏幕朝向改变监听函数 用于在屏幕横竖屏切换时改变保存的图片的方向
*/
private void startOrientationChangeListener() {
OrientationEventListener mOrEventListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int rotation) {
Log.i(TAG, "当前屏幕手持角度方法:" + rotation + "°");
if (((rotation >= 0) && (rotation 315)) {
rotation = 0;
} else if ((rotation > 45) && (rotation 135) && (rotation 225) && (rotation 315 || degrees 45 && degrees 135 && degrees 225 && degrees
关注
打赏