Android开发之dp转像素,像素转换为dp工具类,详细代码,带有源文件下载地址。
    
import android.content.Context;
/** 
 * @author 官网
 *  
 *         David编写: 微博:http://weibo.com/93sec.cc 
 *  
 * @version V1.0正式版 
 *  
 * @process
 *  
 * @Note weibo.com/mcxiaobing 
 *  
 * @dateTime 2015-10-18下午1:46:20 
 *  
 */  
public class DensityUtil {
  /**
   * dip转像素
   */
  public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
  }
  /**
   * 像素转dip
   */
  public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (pxValue / scale + 0.5f);
  }
}