Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - convert dp to float

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?

like image 873
lorraine Avatar asked Nov 28 '12 08:11

lorraine


People also ask

What is dp in 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.

How do you convert dp to PX?

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).

What is dp in Android?

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.

What is dp and PX in Android?

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.


1 Answers

From android.content.res.Resources.getDimension(int id):

float twelveDp = TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 12, 
                mContext.getResources().getDisplayMetrics() );
like image 134
SleepyTonic Avatar answered Sep 30 '22 14:09

SleepyTonic