Android dp和px之间进行转换


 1 public class DensityUtil {  
 2   
 3     /** 
 4      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
 5      */  
 6     public static int dip2px(Context context, float dpValue) {  
 7         final float scale = context.getResources().getDisplayMetrics().density;  
 8         return (int) (dpValue * scale + 0.5f);  
 9     }  
10   
11     /** 
12      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
13      */  
14     public static int px2dip(Context context, float pxValue) {  
15         final float scale = context.getResources().getDisplayMetrics().density;  
16         return (int) (pxValue / scale + 0.5f);  
17     }  
18 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM