What is the difference between px, dp, dip, and dpi ?

Answer: px - pixels, dp - density independent pixels, dip - density independent pixels, sp - scale independent pixels

Description:
px - is meant for absolute pixels. This is used if you want to give in terms of absolute pixels for width or height. Not recommended.

dp/ dip - both mean same, density independent pixels. This is alternative of px. Generally we never use px because it is absolute value. If you use px to set width or height, and if that application is being downloaded into different screen sized devices, then that view will not stretch as per the screen original size. dp is highly recommended to use in place of px.


Use dp if you want to mention width and height to grow shrink dynamically based on screen sizes.
  
if we give dp/dip, android will automatically calculate the pixel size on the basis of 160 pixel sized screen.

sp - Scale independent pixels. when mentioning the font sizes to fit for various screen sizes, use sp. This is similar to dp.Use sp specially for font sizes to grow shrink dynamically based on screen sizes.

dpi- dots per inch. It is the resolution factor. More the dpi better and higher the screen resolution.

Math logic behind it:
1dp = 1px in 160 dpi screen. Means 1 dp is equal to 1physical px of a device having 160 dots per inch.
 

so no of dynamic pixles for a given dp is = (dp's)*(density/160).
For example on 240dpi screen 1dp = 1.5px (physical pixels)

No comments:

Post a Comment