获取手机DPI

        在有些做手机适配方案的时候,需要得知手机dpi,可采用以下代码:



private void printDPI() {
	 String str = "";
	 DisplayMetrics dm = new DisplayMetrics();
	 // getWindowManager().getDefaultDisplay().getMetrics(dm);
	 dm = this.getApplicationContext().getResources().getDisplayMetrics();
	 int screenWidth = dm.widthPixels;
	 int screenHeight = dm.heightPixels;
	 float density = dm.density;
	 float dpi = dm.densityDpi;
	 float widthPixels = dm.widthPixels;
	 float xdpi = dm.xdpi;
	 float ydpi = dm.ydpi;
	
	 str += "屏幕分辨率为:" + dm.widthPixels + " * " + dm.heightPixels + "\n";
	 str += "绝对宽度:" + String.valueOf(screenWidth) + "pixels\n";
	 str += "绝对高度:" + String.valueOf(screenHeight) + "pixels\n";
	 str += "逻辑密度:" + String.valueOf(density) + "\n";
	 str += "dpi:" + String.valueOf(dpi) + "\n";
	 str += "widthPixels:" + String.valueOf(widthPixels) + "\n";
	 str += "X 维 :" + String.valueOf(xdpi) + "像素每英尺\n";
	 str += "Y 维 :" + String.valueOf(ydpi) + "像素每英尺\n";
	
	 Log.i("@@@@@@", str);
	 }


你可能感兴趣的:(获取手机DPI)