My font size is 12dp.
I'm setting the font using TextPaint
, since I'm using a span. The problem is the parameter that TextPaint
accepts is in float. I'm wondering how can I convert 12 dp to float?
dp or Density-Independent Pixels Density-independent pixels are an abstract measurement unit based on the physical density of the screen, which varies depending upon the Android device in question.
For this mapping, 1 dp is considered to be equal to 1 pixel on a 160 dpi resolution screen. The corresponding number of pixels can be calculated with the formula px = dp * (dpi/160).
One dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen (160dpi; the "baseline" density). Android translates this value to the appropriate number of real pixels for each other density.
A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp: dp = (width in pixels *... A dp is equal to one physical pixel on a screen with a density of 160. To calculate dp: dp = (width in pixels * 160) / screen density.
From android.content.res.Resources.getDimension(int id):
float twelveDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 12,
mContext.getResources().getDisplayMetrics() );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With