What is the difference between px, dp, Dip and sp?

Asked

Viewed 22,046 times

21

What’s the difference between px, dp, dip and sp on Android?

2 answers

27


  • px:

DEFINITION: Corresponding to the number of pixels on the screen USE: avoid using px for everything, only in very specific cases is recommended.

  • sp:

DEFINITION: (Scale-Independent Pixels) Idem to dp, but also considers the size of the font the user is using. It is recommended to use this unit when specifying the size of a font, so that it is automatically adjusted according to the preferences of the user screen. USE: Always use sp for fonts!

  • Dip or dp:

DEFINITION: (Density-Independent Pixels) This unit is relative to the screen resolution. For example if the screen resolution is 160 dpi, it means that one dp represents 1 pixel in a total of 160. USE: I advise instead of using px always use dp.


We still have other units of measures used by Android

  • in: (inches)

DEFINITION: Based on the physical size of the screen

  • mm: (millimetre)

DEFINITION: Based on the physical size of the screen

  • pt: (dots)

DEFINITION: 1/72 of an inch, based on the physical size of the screen

More about units of measurements and dimensions on Android here A super explanatory video created by @Netomarin http://www.youtube.com/watch?v=Ocaq1bu3f2w

  • Vlw! Broke a branch!

  • 3

    Good the answer, just adding that 1 dp = 1 px on mdpi screens, so it is easier for you to design your screens. A guide to this can be found at: http://developer.android.com/guide/practices/screens_support.html

  • Detail: some of this information has if you press Ctrl + Space on the property and read on the right in the box that opens with information. as well as for other things

  • Density-Independent pixel is the Google version for pixel replacement, that in the case of mobile devices is a point of such size that if it is seen at a distance that the person holds an appliance, merges with the surrounding points resulting in a continuous visual experience, without distinction between one point and another. The choice of 160 pixels per inch for average density (mdpi) is not arbitrary: it is the density at which a pixel on the screen has the approximate size of a pixel replacement.

4

  • px is pixel.
  • sp is Scale-Independent pixels (scale independent pixels).
  • Dip is Density-Independent pixels (pixels independent of density). enter image description here

Where to use:

  • sp: for the definition of sources;
  • dip for other size definitions.

    dip == dp

In accordance with Android API Guides:

  • dp: It is an abstract unit that is based on the physical density of the screen. These units are relative to 160dpi (points per inch) of screen, where 1dp is approximately equal to 1px. When executed on a higher density screen, the number of pixels used to draw 1dp is scaled by an appropriate screen factor. Similarly, when on a lower density screen, the number of pixels used to 1dp is reduced. The proportion of dp to pixel change with the density of the screen, but not necessarily in direct proportion. Using units dp (instead of pixel drives) is a simple solution for creating views in his layout capable of resizing properly for different screen densities. In other words, it provides consistency to the actual sizes of your user interface elements on different devices.
  • sp: This is like the unit of dp, but it is also scaled by the font size the user’s preference. It is recommended to use this unit when specifying font sizes, so it will be adjusted for both screen density and user preference.
  • px: Corresponds to pixels screen real. This unit of measure is not recommended because the actual representation may vary between devices; each of the devices may have a different number than pixels per inch and may have more or less total of pixels available on screen.

In addition to those described in the question, there are still other units of measure on Android:

  • pt: Dots - 1/72 of an inch based on the physical size of the screen.
  • mm: Millimeters - Based on the physical size of the screen.
  • in: Inches - Based on physical screen size.

The following image helps to describe this relationship between the Android units of measures:

enter image description here

Response based and translated that of Soen;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.