Convert from px to dp for Google nexus 7

I am new to tablet app development. I have a Google Nexus7 with a screen resolution (600 * 960 dip). So, I want to know that it falls into the mdip category? According to this, 1px = 1dp (base level). But if I follow this, it does not look good on the tablet. I create a separate layout folder for nexus7 -sw600dp and specify the sizes according to mdip, but it does not work. My images don't look good either. What I want to know is the actual conversion rate for google nexus 7. Need help.

+8
android nexus-7
source share
3 answers

Formula: pixels = dips * (density / 160)

Nexus 7 has a size of 800x1280 with a density of 213 px, which means the resolution code is tvdpi (which means that you may have a folder called drawable-tvdpi ).

You can measure the size of the available screen, minus the window decorations, using:

 this.getResources().getConfiguration().screenWidthDp; this.getResources().getConfiguration().screenHeightDp; 

On my N7, it returns 600 dp w, 888 dp h. Following the above formula, the height of 888 dp is 1280px - window decoration.

+13
source share

Nexus7 is a unique device with a somewhat strange trend structure.

For nexus 7

 layout-large-hdpi 

Here is a very good explanation (from Dianne Hackborn, an Android engineer at Google): Dianne Hackborn explains the unique resolution of the Nexus7

Note. . The application accepts images from these folders only if you have not received higher priority qualifiers. For example, if you specified a layout folder, such as layout-sw360dp, the application will only accept images from this folder, even if you provided separate layouts similar to what I said above. Because in android there is a priority order in which you have to give layouts.

Screen density

Commonly called dpi (dots per inch). Android groups all actual screen densities into four generalized densities: low (120), medium (160), high (240), and ultrahigh (320). A device such as the Galaxy Nexus has an "ultra-high" screen density (more specifically, the dpi value is set to 320). Nexus 7 uses "tvdpi" - that is, 213 dpi.

Density Independent Pixel

Commonly called dp. This is a virtual pixel element used when displaying content. A density-independent pixel is equivalent to one physical pixel on a screen with a resolution of 160 dpi. To calculate dp, use the following formula:

 px = dp * (dpi / 160) 

or equivalently:

 dp = (px / dpi) * 160 

The reason the Nexus 7 can display more content than the Galaxy Nexus, despite having similar resolutions: dpi for the Nexus 7 is lower than the Galaxy Nexus .

Galaxy Nexus (320 dpi, 720 pixels wide)

 (720 / 320) * 160 = 360 dp 

Nexus 7 (213 dpi, 800 pixels wide)

 (800 / 213) * 160 = 600 dp 

This means that when applications render on the Galaxy Nexus, the screen width is actually 360 dp (displayed using 720 pixels). While on Nexus 7, the screen width is 600 dp (displayed using 800 pixels).

+3
source share

Try to execute the code below, give the pixel value below the method, and you will get the result in dp.

 public int convertSizeToDeviceDependent(int value) { DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); return ((dm.densityDpi * value) / 160); } 
+1
source share

All Articles