1.利用DisplayMetrics对象获取
DisplayMetrics dm =new DisplayMetrics();
dm = getResources().getDisplayMetrics();
float density = dm.density;// 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi;// 屏幕密度(每寸像素:120/160/240/320)
int screenWidth = dm.widthPixels;// 屏幕宽(像素,如:3200px)
int screenHeight = dm.heightPixels;// 屏幕高(像素,如:1280px)
2.WindowManager和DisplayMetrics的结合
DisplayMetrics dm =new DisplayMetrics();
dm =newDisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
float density = dm.density;// 屏幕密度(像素比例:0.75/1.0/1.5/2.0)
int densityDPI = dm.densityDpi;// 屏幕密度(每寸像素:120/160/240/320)
int screenWidthDip = dm.widthPixels;// 屏幕宽(dip,如:320dip)
int screenHeightDip = dm.heightPixels;// 屏幕高(dip,如:533dip)
int screenWidth = (int)(dm.widthPixels * density +0.5f);// 屏幕宽(px,如:720px)
int screenHeight = (int)(dm.heightPixels * density +0.5f);// 屏幕高(px,如:1280px)