Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert DP, PX, SP among each other, especially DP and SP?

I have known the difference among DP, SP and PX. And after searching this topic, I found nothing satisfying me completely. Maybe this post is a duplicate, but I still want to know what is the formula of converting from DP to PX, and DP to SP, from SP to PX, from PX to SP, from SP to DP, from DP to SP? I have known some codes to do this, but they are imperfect.

like image 473
SilentKnight Avatar asked Apr 16 '15 03:04

SilentKnight


People also ask

What is the difference between PX dip dp and SP?

px - one pixel Corresponds to actual pixels on the screen. Pixel is the smallest controllable element of a picture represented on the screen. sp - Scale-independent Pixels This is like the dp unit. scaled by the user's font size preference.

What is the difference between the dp unit and the PX unit?

Definitions. px or dot is a pixel on the physical screen. dpi are pixels per inch on the physical screen and represent the density of the display. dip or dp are density-indenpendant pixels, i.e. they correspond to more or less pixels depending on the physical density.

How many pixels is 1dp?

If device-independent pixel (dp) is used as the unit of length, then the operating system of the device maps the dp value to a corresponding number of pixels based on the resolution of the device screen. For this mapping, 1 dp is considered to be equal to 1 pixel on a 160 dpi resolution screen.


2 Answers

DP to PX:

public static int dpToPx(float dp, Context context) {     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); } 

SP to PX:

public static int spToPx(float sp, Context context) {     return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, context.getResources().getDisplayMetrics()); } 

DP to SP:

public static int dpToSp(float dp, Context context) {     return (int) (dpToPx(dp, context) / context.getResources().getDisplayMetrics().scaledDensity); } 
like image 69
AndroidEx Avatar answered Oct 22 '22 08:10

AndroidEx


The accepted answer is missing a few useful conversions.

SP to PX

float sp = 20; float px = sp * getResources().getDisplayMetrics().scaledDensity; 

or

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics()); 

PX to SP

float px = 70; float sp = px / getResources().getDisplayMetrics().scaledDensity; 

DP to PX

float dp = 20; float px = dp * getResources().getDisplayMetrics().density; 

or

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics()); 

PX to DP

float px = 70; float dp = px / getResources().getDisplayMetrics().density; 

Notes

  • The floats I chose above (20 and 70) were arbitrary values. You can plug in different numbers if you like.
  • px refers to pixels. The number of pixels that a device has per inch of screen space is called the density.
  • dp means density-independent pixels. That is, no matter what device is used, the actual size should be the same. For example, if I set a view to be 100 dp wide, it will have the same width on a new high density phone as it does on an old low density phone. (If I had set the width to 100 px, on the other hand, it would appear large on a low density phone and small on a high density phone.) Density is measured in dots per inch (DPI). The formula is px = dp * density. So you just multiply or divide by the density to convert between px and dp.
  • sp means scale-independant pixels. It is just used for fonts, not views. It is similar to dp except it also factors in the user preferences. This density with user preferences taken into account is known as scaled density. Setting a TextView font to a size of 30 sp, for example, will make the text generally appear to be the same physical size on all devices. However, your grandmother may have her preferred font size maxed all the way up in her phone settings, so 30 sp text will look bigger on her phone than it does on yours. The formula is px = sp * scaledDensity.
  • Meaning of DP and SP
  • DP to SP conversion is not generally useful
like image 34
Suragch Avatar answered Oct 22 '22 09:10

Suragch