Android常用代码(类似工具类吧)

1 手机px和dp相互转换

/**
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
*/
public static int dip2px(Context context, float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}

/**
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
*/
public static int px2dip(Context context, float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}

 

2 获取手机屏幕的大小

http://www.cnblogs.com/bjzhanghao/archive/2012/11/12/2765835.html

 

posted @ 2013-03-19 17:34  bavariama  阅读(263)  评论(0编辑  收藏  举报