android px转换为dip/dp

 

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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。