Skip to content Skip to sidebar Skip to footer

Scale-independent Pixel

I don't know how it convert to the unit of pixels. For example, if I assign 10sp to my font size on 160 dpi device, how much px of my font size is it ? Or, if I assign 10sp to my f

Solution 1:

They're just the same as dip, but they also take into account the font scaling factor that the user sets on his device. So if he/she left font size set to "normal", it's just like dip. If there is a font scaling factor, you can get the whole scaled density from DisplayMetrics.scaledDensity.

(I have to admit I just tried to look for some "font size" option in my Android phone global settings and I couldn't find it, so I wonder if it is of any use right now. Am I missing something?)

Solution 2:

This depends on screen density set for the device. You can obtain it with

screenDensity = this.getResources().getDisplayMetrics().density;

Then by multiplying screenDensity and the size in dp or sp you can get actual pixel size. For example on 320x240 device the creen density is about 0.75, while on HTC Desire HD and Galaxy Tab is 1.5 even though they have different resolution and may not have the same dpi.

Post a Comment for "Scale-independent Pixel"