我如何获得屏幕的宽度和高度,并使用这个值在:

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
    Log.e(TAG, "onMeasure" + widthSpecId);
    setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT - 
        game.findViewById(R.id.flag).getHeight());
}

当前回答

    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;
    }

其他回答

屏幕分辨率是屏幕中像素的总和。下面的程序将提取设备的屏幕分辨率。它将打印屏幕的宽度和高度。这些值以像素为单位。

public static Point getScreenResolution(Context context) {
// get window managers
WindowManager manager =  (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Display display = manager.getDefaultDisplay();
Point point = new Point();
display.getSize(point);

 // get width and height
 int width = point.x;
 int height = point.y;

 return point;

}

为什么不

DisplayMetrics displaymetrics = getResources().getDisplayMetrics();

然后使用

displayMetrics.widthPixels

and

displayMetrics.heightPixels

完整的方法是使用“getRealSize”来返回真实的分辨率(包括当用户改变分辨率时)。

我注意到所有其他可用的函数,包括文档中说要用的代替this的函数,在某些情况下结果会更小。

下面是代码:

            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Point size = new Point();
            wm.getDefaultDisplay().getRealSize(size);
            final int width = size.x, height = size.y;

因为这可以在不同的方向上改变,这里有一个解决方案(在Kotlin中),无论朝向如何,都能得到正确的结果:

/**
 * returns the natural orientation of the device: Configuration.ORIENTATION_LANDSCAPE or Configuration.ORIENTATION_PORTRAIT .<br></br>
 * The result should be consistent no matter the orientation of the device
 */
@JvmStatic
fun getScreenNaturalOrientation(context: Context): Int {
    //based on : http://stackoverflow.com/a/9888357/878126
    val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val config = context.resources.configuration
    val rotation = windowManager.defaultDisplay.rotation
    return if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && config.orientation == Configuration.ORIENTATION_LANDSCAPE || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && config.orientation == Configuration.ORIENTATION_PORTRAIT)
        Configuration.ORIENTATION_LANDSCAPE
    else
        Configuration.ORIENTATION_PORTRAIT
}

/**
 * returns the natural screen size (in pixels). The result should be consistent no matter the orientation of the device
 */
@JvmStatic
fun getScreenNaturalSize(context: Context): Point {
    val screenNaturalOrientation = getScreenNaturalOrientation(context)
    val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val point = Point()
    wm.defaultDisplay.getRealSize(point)
    val currentOrientation = context.resources.configuration.orientation
    if (currentOrientation == screenNaturalOrientation)
        return point
    else return Point(point.y, point.x)
}
DisplayMetrics dimension = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dimension);
        int width = dimension.widthPixels;
        int height = dimension.heightPixels;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
  public static double getHeight() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    display.getRealMetrics(displayMetrics);

    //int height = displayMetrics.heightPixels;
    //int width = displayMetrics.widthPixels;
    return displayMetrics.heightPixels;
  }

使用这种方法你可以得到屏幕高度。 如果你想要宽度改变displayMetrics。hightpixels到displayMetrics.widthPixels。

它还包括所需的api构建版本。