27

Possible Duplicate:
converting pixels to dp in android

I'm trying to convert pixels to dp. What is the formula?

Lets convert 640 and 480 into dp. The docs say this

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

But I don't think that is what I need (and I don't know how to use this). I guess I just need the forumla. I have the code ready:

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

    switch(metrics.densityDpi)
    {
         case DisplayMetrics.DENSITY_LOW:
         int sixForty = ?
         int fourEighty = ?
         break;

         case DisplayMetrics.DENSITY_MEDIUM:
         int sixForty = ?
         int fourEighty = ?
         break;

         case DisplayMetrics.DENSITY_HIGH:
         int sixForty = ?
         int fourEighty = ?
         break;
    }
Community
  • 1
  • 1
spentak
  • 4,627
  • 15
  • 62
  • 90
  • 1
    If you're looking to do a one-off conversion (for instance for exporting sprites from Photoshop), [here's a nifty converter](https://pixplicity.com/dp-px-converter/). – Paul Lammertsma Jul 09 '15 at 13:22

1 Answers1

82

Instead of trying to infer the dp conversion factor from the screen's density classification, you can simply query it directly:

getWindowManager().getDefaultDisplay().getMetrics(metrics);
float logicalDensity = metrics.density;

logicalDensity will then contain the factor you need to multiply dp by to get physical pixel dimensions for the device screen.

int px = (int) Math.ceil(dp * logicalDensity);
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
mportuesisf
  • 5,587
  • 2
  • 33
  • 26
  • i need the dp value of PIXELS. I have a video that is 640pixels by 480pixels and I need to get their dp equivalents based upon what density the screen is (I know how to get pixels from dp, but not vice versa) – spentak Jul 11 '11 at 21:27
  • 9
    To get dp from pixels, divide the density into the pixel value rather than multiply. – mportuesisf Jul 11 '11 at 21:29
  • 13
    public int convertToDp(int input) { // Get the screen's density scale final float scale = getResources().getDisplayMetrics().density; // Convert the dps to pixels, based on density scale return (int) (input * scale + 0.5f); } – neeraj t Mar 28 '12 at 09:40
  • 7
    Old question, but why + 0.5 ? – Idistic May 04 '12 at 03:33
  • 5
    The +0.5 rounds up to the nearest whole number if necessary. When converted to integer, everything after the decimal is simply cut off. The +0.5 ensures that the most "correct" integer is returned. – mportuesisf May 04 '12 at 16:06
  • 7
    I would use `int px = (int) Math.ceil(dp * logicalDensity);` then. At least for educational purposes :-) – dunadar Feb 14 '13 at 00:31
  • 1
    Its really odd the ceil function returns a double. – atreat Jun 04 '13 at 14:43
  • I finally made the edit to use a proper Math function instead of `+ 0.5`... – Austyn Mahoney Jan 07 '14 at 23:08
  • I have created online calculator( converter ) you can check it out http://servoper.net/android-design-calculator/ – Sir NIkolay Cesar The First Feb 16 '15 at 15:48