Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Android support dp's less than 1dp?

Tags:

android

I am making a calendar and I need a grid. Problem is the borders, or rather the space between each grid, which is what I am using to kind of simulate a grid, is 1 dp. But its rather thick. I am looking at other calendar apps that have the borders, and they are very thin. Even if I were to use a drawable shape and make it 1dp, it still has that thickness. I tried using .5, but that did not seem to work. Is that not possible?

like image 376
Andy Avatar asked Jul 22 '12 05:07

Andy


People also ask

What is dp in XML?

Density-independent pixel (dp) A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.


2 Answers

Android does allow you to enter fractional values for dp into XML, though I'm not sure I would recommend it because the results become less easy to predict/compute. Devices convert dp values into pixels using (basically) the following formula:

px = (int)(scale * dp + 0.5) 

(i.e. the device density scale rounded to the nearest whole pixel value)

The scale value would be based on the screen density of the device:

  • MDPI = 1
  • HDPI = 1.5
  • XHDPI = 2
  • XXHDPI = 3

So 0.5dp would result in {1px, 1px, 1px, 2px} for the above densities, whereas 1dp would be {1px, 2px, 2px, 3px}. A tiny value like 0.1dp would resolve to {1px, 1px, 1px, 1px} because anything less than 1 in the above formula resolves to a single pixel unless the value was explicitly 0dp (it's not possible to draw something thinner than a single pixel width).

If you want to ensure that thinnest possible width is used, probably best to define the width explicitly with px instead of a scaled unit like dp. Setting a value of 1px will draw with a single pixel on all devices, and is much more readable than hoping 0.5dp or 0.1dp does the same.

like image 63
devunwired Avatar answered Sep 23 '22 02:09

devunwired


Yes.

I set to 0.1dp and it is smaller than 1dp on my 4" ME860 DRIOD.

like image 45
Houcheng Avatar answered Sep 23 '22 02:09

Houcheng