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

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

当前回答

我发现weigan的答案最好的一个在这一页,这里是如何在Xamarin使用它。Android:

public int GetScreenWidth()
{
    return Resources.System.DisplayMetrics.WidthPixels;
}

public int GetScreenHeight()
{
    return Resources.System.DisplayMetrics.HeightPixels;
}

其他回答

由于getMetrics和getRealMetrics已弃用,谷歌建议按如下方式确定屏幕宽度和高度:

WindowMetrics windowMetrics = getActivity().getWindowManager().getMaximumWindowMetrics();
Rect bounds = windowMetrics.getBounds();
int widthPixels = bounds.width();
int heightPixels = bounds.height();

然而,我想出了另一种方法,给我同样的结果:

Configuration configuration = mContext.getResources().getConfiguration();
Display.Mode mode = display.getMode();
int widthPixels = mode.getPhysicalWidth();
int heightPixels = mode.getPhysicalHeight();

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

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 = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

在某些情况下,这可能行不通。获取getMetrics注释:

Gets display metrics that describe the size and density of this display. The size returned by this method does not necessarily represent the actual raw size (native resolution) of the display. The returned size may be adjusted to exclude certain system decor elements that are always visible. It may be scaled to provide compatibility with older applications that were originally designed for smaller displays. It can be different depending on the WindowManager to which the display belongs. If requested from non-Activity context (e.g. Application context via (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE)) metrics will report the size of the entire display based on current rotation and with subtracted system decoration areas. If requested from activity (either using getWindowManager() or (WindowManager) getSystemService(Context.WINDOW_SERVICE)) resulting metrics will correspond to current app window metrics. In this case the size can be smaller than physical size in multi-window mode.

所以,要得到真正的大小:

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;

or:

Point point = new Point();
getWindowManager().getDefaultDisplay().getRealSize(point);
int height = point.y;
int width = point.x;

为什么不

DisplayMetrics displaymetrics = getResources().getDisplayMetrics();

然后使用

displayMetrics.widthPixels

and

displayMetrics.heightPixels

试试下面的代码:-

1.

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

2.

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

or

int width = getWindowManager().getDefaultDisplay().getWidth(); 
int height = getWindowManager().getDefaultDisplay().getHeight();

3.

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

metrics.heightPixels;
metrics.widthPixels;