这是一个创建于 3161 天前的主题,其中的信息可能已经有所发展或是发生改变。
/**
* dip to px
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
/**
* px to dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
}
关于转换中 0.5f 起什么作用?
2 条回复 • 2015-11-02 15:25:45 +08:00
![menc](https://cdn.v2ex.com/avatar/20e7/9c43/116167_normal.png?m=1447945679) |
|
1
menc 2015-11-02 14:44:18 +08:00 via iPhone
四舍五入
|
![stdying](https://cdn.v2ex.com/avatar/8eec/7d1b/36090_normal.png?m=1719541770) |
|
2
stdying 2015-11-02 15:25:45 +08:00
|