Get screen dimensions in pixels

2018-12-30 23:54发布

I created some custom elements, and I want to programmatically place them to the upper right corner (n pixels from the top edge and m pixels from the right edge). Therefore I need to get the screen width and screen height and then set position:

int px = screenWidth - m;
int py = screenHeight - n;

How do I get screenWidth and screenHeight in the main Activity?

30条回答
旧人旧事旧时光
2楼-- · 2018-12-31 00:18

For who is searching for usable screen dimension without Status Bar and Action Bar (also thanks to Swapnil's answer):

DisplayMetrics dm = getResources().getDisplayMetrics();
float screen_w = dm.widthPixels;
float screen_h = dm.heightPixels;

int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
    screen_h -= getResources().getDimensionPixelSize(resId);
}

TypedValue typedValue = new TypedValue();
if(getTheme().resolveAttribute(android.R.attr.actionBarSize, typedValue, true)){
    screen_h -= getResources().getDimensionPixelSize(typedValue.resourceId);
}
查看更多
千与千寻千般痛.
3楼-- · 2018-12-31 00:19

Just adding to Francesco's answer. The other observer that is more apt, if you want to find out the location in window or location in screen is ViewTreeObserver.OnPreDrawListener()

This also can be used to find other attributes of a view that is mostly unknown at onCreate() time e.g. the scrolled position, the scaled position.

查看更多
栀子花@的思念
4楼-- · 2018-12-31 00:19

There is a non-deprecated way to do this using DisplayMetrics (API 1), that avoids the try/catch messiness:

 // initialize the DisplayMetrics object
 DisplayMetrics deviceDisplayMetrics = new DisplayMetrics();

 // populate the DisplayMetrics object with the display characteristics
 getWindowManager().getDefaultDisplay().getMetrics(deviceDisplayMetrics);

 // get the width and height
 screenWidth = deviceDisplayMetrics.widthPixels;
 screenHeight = deviceDisplayMetrics.heightPixels;
查看更多
只若初见
5楼-- · 2018-12-31 00:20

I would wrap the getSize code like this:

@SuppressLint("NewApi")
public static Point getScreenSize(Activity a) {
    Point size = new Point();
    Display d = a.getWindowManager().getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        d.getSize(size);
    } else {
        size.x = d.getWidth();
        size.y = d.getHeight();
    }
    return size;
}
查看更多
永恒的永恒
6楼-- · 2018-12-31 00:21

One way is:

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

It is deprecated, and you should try the following code instead. The first two lines of code gives you the DisplayMetrics objecs. This objects contains the fields like heightPixels,widthPixels.

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

int height = metrics.heightPixels;
int width = metrics.widthPixels;
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 00:21

For accessing the height of the status bar for Android devices, we prefer a programmatic way to get it:

Sample code

int resId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resId > 0) {
    result = getResources().getDimensionPixelSize(resId);
}

The variable result gives the height in the pixel.

For quick access

Enter image description here

For more information about height of Title bar, Navigation bar and Content View, kindly look on Android Device Screen Sizes.

查看更多
登录 后发表回答