Android: Get the screen resolution / pixels as integer values

前端 未结 5 1481
再見小時候
再見小時候 2020-12-05 08:38

this is a really simple question on which I\'ve found no answer :/ How can I quickly access the screen resolution (width, height) as integer values?

I\'ve tried this

5条回答
  •  温柔的废话
    2020-12-05 09:15

    The trashkalmar answer is correct.

    However, the results will be specific to the activity context. If you want the whole device screen resolution:

    DisplayMetrics displayMetrics = new DisplayMetrics();
    WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
    wm.getDefaultDisplay().getMetrics(displayMetrics);
    int screenWidth = displayMetrics.widthPixels;
    int screenHeight = displayMetrics.heightPixels;
    

    Note that the results also depend on the current device orientation.

提交回复
热议问题