2

I am assuming that the terminology Density-independent pixels and Device-independent pixels, have same meaning.

Density-independent pixels(dip) are a virtual pixel unit, equivalent to one physical pixel on a 160dpi (MDPI) screen.

4dip is equivalant to 320dpi (XHDPI) screen, because the number of pixels used to render same data on 320dpi screen is 4(2 pixels across and 2 pixels down).

How do I compute pixel units in dip for 240dpi (HDPI) screen?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • Duplicate?: http://stackoverflow.com/questions/5591868/android-pixels-to-dips/16660172#16660172 – Kuffs Nov 18 '15 at 06:47

3 Answers3

1

HDPI is x1.5 for MDPI.

That is 160x1.5 = 240.

On MDPI device, which is standard for dip unit, 1dip is 1px. On HDPI device, physically same size 1dip is 1.5px.

When you at first know some px value, you can convert it into dip by deviding by its scale, for example, so HDPI's scale is 1.5 that you can devide px value by 1.5.

For the same 2dip, HDPI has 3px across and down while MDPI has 2px across and down.

2dip => 2px on MDPI
2dip => 2 x 1.5 = 3px on HDPI

The concept of dip is that 1 dip means physical size of 1/160 inch approxmately, it's a common physical size through all over the devices that have different pixel densities.

hata
  • 11,633
  • 6
  • 46
  • 69
  • what is the value in `dip` units? `1.5dip`? If yes, How do I interpret `1.5dip`? I mean `4dip` is 2 pixels across and 2 pixels down. – overexchange Nov 18 '15 at 06:04
  • i am still not clear. How do I interpret, when I say 3dip on MDPI? Is it 3 physical pixels on a 160dpi screen? If yes, what does this mean? – overexchange Nov 18 '15 at 12:37
  • @overexchange *when I say 3dip on MDPI, Is it 3 physical pixels on a 160dpi screen?* Yes, it is. – hata Nov 18 '15 at 12:56
  • So, What does it mean, when I say, *on non-retina iPhones there are 320 physical pixels and also 320 dips*? Mainly, here am confused, when I say 320 dips. Please help me!!! Because I do not understand the meaning of 3 physical pixels on 160dpi screen. What does it mean to say 3 physical pixels on 160dpi screen? Why are we measuring this way?I could not understand the media queries in terms of dpr [here](https://css-tricks.com/snippets/css/retina-display-media-query/)? – overexchange Nov 18 '15 at 13:23
  • So non-retina iPhones' screen densities are approximately 160dpi (MDPI) that you can say 320px has a size of 320dip and 3px has 3dip. The reason is to be physical size as primary and px as secondary (dip stays common and px varies for devices). – hata Nov 18 '15 at 13:27
  • But in this [video](https://www.youtube.com/watch?v=zhszwkcay2A), it says `160dpi` is baseline density. So, accordingly, `1dip` is `160dpi`, where as `2dip` is `320dpi`. So, `320dip` mean `320*160dpi`. This is, how I convert dip to dpi. Now, you say 320dip is 320px which is confusing – overexchange Nov 18 '15 at 13:34
  • No, dpi (or ppi) is density (dot per inch or pixel per inch), not a size, it's a kind of ratio. dip is a unit of size, dpi is ratio. dpi is a fixed value on a device. – hata Nov 18 '15 at 13:45
  • Do you mean 1 dip is 1 px, 2 dip is 2px, 100 dip is 100px, If so, What is the advantage of measuring in dips? – overexchange Nov 18 '15 at 13:50
  • No, 1dip = 1px is only on MDPI devices. For example on XDPI it is 2px. px size can vary on different dpi devices. While dip doesn't. – hata Nov 18 '15 at 13:54
  • OK. So, *Retina iPhones have a width of 640 physical pixels* But these phones have `320dips` because this is 320dpi (XDPI) screen. So DPR = 640/320 = 2. Is that correct? So, to measure in terms of dips one has to know the dpi of screen? – overexchange Nov 18 '15 at 14:05
  • Yes. You need dpr (derived from dpi) to calculate dip from px. – hata Nov 18 '15 at 14:19
  • So, Is dip concept mainly used for android devices? for ex: css syntax - `@media (-webkit-min-device-pixel-ratio: 2) {}` or Do we also use for non-android tablets/phones/desktops? – overexchange Nov 18 '15 at 14:24
  • I don't know but I think it is for mobile touch-based devices. Because our finger tips' size don't change. – hata Nov 18 '15 at 14:29
  • Similar question [here](https://stackoverflow.com/questions/33783645/how-do-i-visualise-dip-unit-for-hdpi-screen). Can you help me? – overexchange Nov 18 '15 at 15:14
0

You can use DPI calculators like this one.You can check http://petrnohejl.github.io/Android-Cheatsheet-For-Graphic-Designers/#screen-densities-and-icon-dimensions for the DPI scale ratio. Using this, HDPI = MDPI x 1.5 so in your case, the value will be 240

jomartigcal
  • 813
  • 1
  • 6
  • 11
0

You can use this function to get a pixel value from dp in consideration of the current device metrics.

public int dpToPx(int dp){
    return Math.round(dp*(getResources().getDisplayMetrics().xdpi/ DisplayMetrics.DENSITY_DEFAULT));
}
dabo248
  • 3,367
  • 4
  • 27
  • 37