0
点赞
收藏
分享

微信扫一扫

Java&Android像素px、dip转换工具类


复制粘贴即可使用,简单粗暴,写完收工。

public class DipUtils {

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

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


举报

相关推荐

0 条评论