简体   繁体   中英

Android understanding screen sizes

I have an emulator with a screen resolution of 720X1280 and a density of 320dpi.

Now, when I use the following code to find out dpheight, dpwidth and density

    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);

    float density  = getResources().getDisplayMetrics().density;
    float dpHeight = outMetrics.heightPixels / density;
    float dpWidth  = outMetrics.widthPixels / density;

I get the following values:

06-24 06:47:46.027 D/Density ( 1003): 2.0
06-24 06:47:46.027 D/DPHeight( 1003): 592.0
06-24 06:47:46.027 D/DPWidth ( 1003): 360.0

I can't make sense of it. Can someone shade some light on it?
I intend to develop an app with loads of text, and I want to set the text sizes appropriately so that it looks consistent.

And yes, I have gone through Google's documentation regarding this.
But I couldn't understand, hence this question.

Density 2.0 is your friend. That's the base dpi ( 160 dpi = mdpi) multiplier . In facts: 320 / 160 = 2.0

[EDIT]

For clarity:

If your emulator is    then the density is    which actually is  

xxxhdpi                4.0                    640 dpi
 xxhdpi                3.0                    480 dpi
  xhdpi                2.0                    320 dpi
   hdpi                1.5                    240 dpi
   mdpi                1.0                    160 dpi
   ldpi                0.75                   120 dpi

2.0 is the ratio of pixels / dp of your screen. So to get the size of your screen in DP , you need to take it's size in pixels and divide it by your Density (2.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM