Convert 40px to sp

What is the sp value for 40px? Please also advise the conversion formula.

I am trying to convert the font sizes present in psd to sp elements for mdpi density, i.e. 320 * 480 at 160dpi

+3


source to share


2 answers


case COMPLEX_UNIT_SP:
     return value * metrics.scaledDensity;

      

from source TypedValue.applyDimension ()



This will convert sp to px, just transpose it and you have the formula to convert px to sp:

sp = px / DisplayMetrics.scaledDensity

      

+13


source


px = dp * (dpi / 160)

      

This does not convert your sp to px or vice versa. But sp and dp are almost similar and hence I find it safe to use this formula.



Density Independent Pixel (dp)     A virtual pixel block that you must use when defining a UI layout to express the dimensions or position of the layout regardless of density.

A density-independent pixel is equivalent to one physical pixel per screen at 160 dpi, which is the baseline density the system assumes for a medium-density screen. At run time, the system transparently handles any scaling of dp blocks, if needed, based on the actual density of the screen being used. Dp transformation blocks in screen pixels are simple px = dp * (dpi / 160)

. For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp when defining your application's UI to ensure your UI displays correctly on different screen densities.

Source: Support for multiple screens .

+2


source







All Articles