0

I want to convert a font size 11sp to font size pt in xhdpi. The ratio for xhdpi should be 1:2.

So, the calculation expected should be like:

11sp * 2 = 22pt (font)

But, it does not match actual font size in Zeplin UI. The font size should look like 14-16 pt.

How to calculate the correct font size in pt from sp?

[Reply to the duplicate issue.] How do I convert pt to sp?

I did search for this question and look though it before. It said the calculation should be 11 X 2. I understand and agree this calculation. But the UI do not agree this calculation. Can you let me know is there any wrong in my understanding?

Community
  • 1
  • 1
sunny tam
  • 97
  • 1
  • 7
  • 1
    Duplicate of http://stackoverflow.com/questions/13404377/how-do-i-convert-pt-to-sp – Justin Mitchell Dec 12 '16 at 03:11
  • Why would you convert from device friendly sp/dp to pt on xhdpi? What about other screen sizes? 22pt will not render the same, even at xhdpi across different xhdpi devices due to resolutions, densities and quality. – Justin Mitchell Dec 12 '16 at 03:19
  • actually, I am writing a backend for appending text to a image. I am not writing mobile. In the library, I need to pass font size in pt format. – sunny tam Dec 12 '16 at 03:31
  • Font size will depend on the device that's using it. You can't expressly convert `sp` to `pt` and expect the same result. You have to convert using `TypedValue.applyDimension()` – Justin Mitchell Dec 12 '16 at 03:34
  • So, I am writing an image generation method in php based backend. I am targeting to generate a image for android use. And the design is using dp and sp as normal android does. For the dp part, dp x 2 = pixel work well. But sp x 2 = pt is not working. Is there any formula I can find out the font size in pt. – sunny tam Dec 12 '16 at 03:43

1 Answers1

0

Using my previous comment, you need to convert to px and when you call your api, send the px and the dpi to convert to pt. Px is the most accurate device independent measurement for converting to other formats, but you must record the dpi otherwise your coversion will be off.

sp > px Api call (px,dpi) px > pt Do stuff Return image

Update: Converting from sp to px

int mySpValue = 123;
Resources r = mContext.getApplicationContext().getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, mySpValue, r.getDisplayMetrics());

Fetch DPI: getting the screen density programmatically in android?

DisplayMetrics metrics = getResources().getDisplayMetrics();
Community
  • 1
  • 1
Justin Mitchell
  • 665
  • 6
  • 13
  • This really help. The calculation finally find out that pt = 16.5. It is really close to the actual font size – sunny tam Dec 12 '16 at 04:19