Get screen width and height in Android

前端 未结 30 3680
野的像风
野的像风 2020-11-22 09:28

How can I get the screen width and height and use this value in:

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
    Log.e(TAG, \"onM         


        
30条回答
  •  长发绾君心
    2020-11-22 09:58

        int getScreenSize() {
            int screenSize = getResources().getConfiguration().screenLayout &
                    Configuration.SCREENLAYOUT_SIZE_MASK;
    //        String toastMsg = "Screen size is neither large, normal or small";
            Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
            int orientation = display.getRotation();
    
            int i = 0;
            switch (screenSize) {
    
                case Configuration.SCREENLAYOUT_SIZE_NORMAL:
                    i = 1;
    //                toastMsg = "Normal screen";
                    break;
                case Configuration.SCREENLAYOUT_SIZE_SMALL:
                    i = 1;
    //                toastMsg = "Normal screen";
                    break;
                case Configuration.SCREENLAYOUT_SIZE_LARGE:
    //                toastMsg = "Large screen";
                    if (orientation == Surface.ROTATION_90
                            || orientation == Surface.ROTATION_270) {
                        // TODO: add logic for landscape mode here
                        i = 2;
                    } else {
                        i = 1;
                    }
    
    
                    break;
                case Configuration.SCREENLAYOUT_SIZE_XLARGE:
                    if (orientation == Surface.ROTATION_90
                            || orientation == Surface.ROTATION_270) {
                        // TODO: add logic for landscape mode here
                        i = 4;
                    } else {
                        i = 3;
                    }
    
                    break;
    
    
            }
    //        customeToast(toastMsg);
            return i;
        }
    

提交回复
热议问题