Android中dp和px之间进行转换

来源:互联网 发布:域名的作用是什么 编辑:程序博客网 时间:2024/06/18 10:56

以下是一个应用类,方便进行px和dp之间的转换。


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

---------------------------------------------------------------------------

GL(arui319)

http://blog.csdn.net/arui319

0 0