开发者

scale-independent pixel

开发者 https://www.devze.com 2023-02-16 18:04 出处:网络
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 font on 240 dpi devi

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 font on 240 dpi device, how much px of my font size开发者_如何学Python is it ?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消