下文来自:
http://blog.youkuaiyun.com/zhjp4295216/article/details/5979340
-------------------------------------------------
本文给大家总结了Android px和dip及sp的区别及转换方法,并附有简短的代码演示,希望能够对大家进行代码转换时有重要的作用。
Java代码:
publicstaticintdip2px(Context context, float dipValue){
final float scale=context.getResources().getDisplayMetrics().density;
return (int)(dipValue*scale+ 0.5f);
}
publicstaticintpx2dip(Context context, float pxValue){
final float scale=context.getResource().getDisplayMetrics().density;
return (int)(pxValue/scale+ 0.5f);
}
px (pixels)像素 -- 一般我们HVGA代表320x480像素,这个用的比较多。
dip或dp (device independent pixels)设备独立像素 -- 这个和设备硬件有关,一般我们为了支持WVGA、HVGA和QVGA cwj推荐使用这个,不依赖像素。
sp (scaled pixels — best for text size)放大像素-- 主要处理字体的大小。
下面的几个是不常用的,大家也知道。这里就不再过多的赘述,希望对你有所帮助!
in (inches)英寸
mm (millimeters)毫米
pt (points)点
This blog post provides a summary on the distinctions between Android's pixel (px), device-independent pixels (dip or dp), and scaled pixels (sp), along with code snippets for their conversion. Essential for developers aiming to enhance their understanding of screen size adaptation in Android applications.
1290

被折叠的 条评论
为什么被折叠?



