Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why dpToPixel should not be used to set layout dimensions?

Why in javadocs for dpToPixel (declared here) is stated that it shouldn't be used to set layout dimensions?

like image 678
pepyakin Avatar asked May 18 '15 10:05

pepyakin


People also ask

What is difference between dp and PT?

pt Points - 1/72 of an inch based on the physical size of the screen. dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen.

What is the difference between px and dp when would you use SP?

PX: is an abbreviation for Pixels, which specifies the actual pixels on the screen. SP: is an abbreviation for Scale independent pixels. It is the same as the dp unit, but it is additionally scaled according to the user's font size selection.

What is the difference between the SP and dp dimensions in Android?

The sp unit is the same size as dp, by default, but it resizes based on the user's preferred text size.

Where do we use dp and SP?

You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities. SP — this is like the dp unit, but it is also scaled by the user's font size preference.


2 Answers

We don't use pixels to define layout dimensions. Using density independent pixels ensures proper layout sizes across a variety of devices. Meaning said layout will have roughly the same visual size on a 4" phone and a 7" tablet. (Which is completely unrelated to the problem at hand :) )

That being said the actual layout sizes (e.g. LayoutParams class) are in fact using whole pixels to define the resulting size. It is viable to use the dpToPixels method in this way:

float px = dpToPixels(16, getResources()); // 16 dp to pixels precise
int pxOffset = (int) px; // 16dp rounded down to whole pixels
int pxSize = (int) (0.5f + px); // 16dp rounded up to whole pixels

Now you can use these values in layouts, pxOffset for padding, margin etc. (can be zero) and pxSize for width and height (ensures at least 1px size unless zero). The same calculation is done when using methods int Resources.getDimensionPixelOffset(int) and int Resources.getDimensionPixelSize(int) which are suitable for use with layouts and float Resources.getDimension(int) which is suitable for drawing precisely.

EDIT

Elevation uses float values so using the precise dimension is completely fine.

TranslationX, translationY or translationZ are defined using float values. These and many more view properties are used for animation so it makes sense to use smooth values. If set by hand use whole integers for predictable behavior.

like image 142
Eugen Pechanec Avatar answered Nov 15 '22 01:11

Eugen Pechanec


px Pixels - point per scale corresponds to actual pixels on the screen.

dp Density-independent Pixels - an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi screen, so one dp is one pixel on a 160 dpi screen. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Note: The compiler accepts both "dip" and "dp", though "dp" is more consistent with "sp".

It is very easy to understand what px is, for screens of 1920*1080, 1440*960...But there is density concept in Android.

**px = density*density**.

As Android developers, we all know Android Fragmentation Problem. Google recommend us to use dip to layout our views but not px. But sometimes we have to use px. So, we need this conversion.

PS: PX and DP are different but similar.

DP is the resolution when you only factor the physical size of the screen. When you use DP it will scale your layout to other similar sized screens with different pixel densities.

Occasionally you actually want pixels though, and when you deal with dimensions in code you are always dealing with real pixels, unless you convert them.

So on a android device, normal sized hdpi screen, 800x480 is 533x320 in DP (I believe). To convert DP into pixels /1.5, to convert back *1.5. This is only for the one screen size and dpi, it would change depending on design. Our artists give me pixels though and I convert to DP with the above 1.5 equation.

like image 21
SilentKnight Avatar answered Nov 15 '22 00:11

SilentKnight