0

I found this in the Android documentation:

The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160)

But I don't understand what dpi is and how it is calculated. Any help?

Niall C.
  • 10,878
  • 7
  • 69
  • 61
Adham
  • 63,550
  • 98
  • 229
  • 344

4 Answers4

2

dpi stands for Dots Per Inch. In android it is used to denote screen density of the mobile.

Android smartphones support 4 types of dpi

  1. Low density (120), ldpi
  2. Medium density (160), mdpi
  3. High density (240), hdpi
  4. Extra high density (320), xhdpi

You can get info on the display from the DisplayMetrics struct:

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

Visit http://developer.android.com/guide/practices/screens_support.html for more information about screen sizes.

Jagdeep Singh
  • 885
  • 6
  • 18
1

dpi stands for dots-per-inch and is a physical characteristic of any screen.

You can access the x and y dpi values by examining the xdpi and ydpi fields of the DisplayMetrics class. To get the DisplayMetrics use something like the following...

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float x = metrics.xdpi; // Retrieve the x dpi value
float y = metrics.ydpi; // Retrieve the y dpi value
Squonk
  • 48,735
  • 19
  • 103
  • 135
1

Perhaps playing with a DP calculator will illustrate how this works. This nifty converter demonstrates it best, in my opinion.

It illustrates how pixel dimensions appear as different physical dimensions between screens of different densities. For instance, 48px will appears half as big on a screen with twice the DPI.

There are various DPI bins known to Android:

  • ldpi, 120 dpi
  • mdpi, 160 dpi
  • tvdpi, 213 dpi
  • hdpi, 240 dpi
  • xhdpi, 320 dpi
  • xxhdpi, 480 dpi
  • xxxhdpi, 640 dpi

The baseline DPI is 160 (mdpi), at which 1dp = 1px. At twice that value, 320 (xhdpi), 1dp = 2px.

Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
0

Please go through this post, i think you will get your answer :)

What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
Manoj Pal
  • 170
  • 1
  • 1
  • 11