Freeze screen orientation

前端 未结 3 1951
心在旅途
心在旅途 2021-01-22 13:52

There is CheckBox with following code:

    CheckBox cb = (CheckBox)findViewById(R.id.freezer);
    cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChan         


        
3条回答
  •  猫巷女王i
    2021-01-22 14:47

    So, here is my solution.

    private int getCurentOrientation() {
        Display d = ((WindowManager) getSystemService(WINDOW_SERVICE))
                .getDefaultDisplay();
        boolean isWide = d.getWidth() >= d.getHeight();
        switch (d.getRotation()) {
        case Surface.ROTATION_0:
            return isWide ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                    : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        case Surface.ROTATION_90:
            return isWide ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
                    : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_180:
            return isWide ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                    : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
        case Surface.ROTATION_270:
            return isWide ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
                    : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
        return -1;
    }
    
    private void lockOrientation(boolean lock) {
        if (lock) {
            setRequestedOrientation(getCurentOrientation());
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
        }
    }
    

    On 4 devices (2 smartphones and 2 tablets) it works as nedded.

提交回复
热议问题