分辨率和像素转换

 1 public final class DensityComputeTool {
 2 
 3     private Context context;
 4 
 5     private DensityComputeTool(Context context) {
 6         this.context = context;
 7     }
 8     
 9     /**
10      * DensityComputeTool.build(context).px2dip(54);
11      * @param context
12      * @return
13      */
14     public static DensityComputeTool build(Context context) {
15         return new DensityComputeTool(context);
16     }
17 
18     /**
19      * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
20      * 
21      * @param dpValue
22      * @return
23      */
24     public int dip2px(float dpValue) {
25         final float scale = context.getResources().getDisplayMetrics().density;
26         return (int) (dpValue * scale + 0.5f);
27     }
28 
29     /**
30      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
31      * 
32      * @param pxValue
33      * @return
34      */
35     public int px2dip(float pxValue) {
36         final float scale = context.getResources().getDisplayMetrics().density;
37         return (int) (pxValue / scale + 0.5f);
38     }
39 }
原文地址:https://www.cnblogs.com/cbooy/p/4739321.html