android dp值的确定

对一款Android手机来说,由硬件配置决定长宽的dp值。
手机的分辨率是确定的,dpi也是确定的,进而可以得到手机的长宽dp值

private void  getWH(){ 
  //获取分辨率方式1   
  DisplayMetrics dm = new DisplayMetrics();   
 getWindowManager().getDefaultDisplay().getMetrics(dm);    
  int width = dm.widthPixels;    
  int height = dm.heightPixels;    

  //获取分辨率方式2
  int screenWidth;    
  int screenHeight;    
  WindowManager windowManager = getWindowManager();    
  Display display = windowManager.getDefaultDisplay();    
  screenWidth = display.getWidth();    
  screenHeight = display.getHeight();    
  final float scale = getResources().getDisplayMetrics().densityDpi;    
  int dipX =  (int)(screenWidth / scale + 0.5f);    
  int dipY = (int)(screenHeight / 4 + 0.5f);    
  Toast.makeText(this,"w:"+dipX+",h:"+dipY,Toast.LENGTH_SHORT).show();
}

你可能感兴趣的:(android dp值的确定)