Android学习 之 获取手机屏幕像素

有三种方法,其实都是一样的,只是做了不同的封装:

 

private void getPhoneDisplayInfo(){
    	// 1.
    	DisplayMetrics displayMetrics1 = new DisplayMetrics();
    	getWindowManager().getDefaultDisplay().getMetrics(displayMetrics1);
    	int screenWidth = displayMetrics1.widthPixels;
    	int screenHeight = displayMetrics1.heightPixels;
    	Log.d("debug", "screenWidth = "+screenWidth+"|screenHeight = "+screenHeight);
    	
    	// 2.
    	DisplayMetrics displayMetrics2 = new DisplayMetrics();
    	displayMetrics2 = getApplicationContext().getResources().getDisplayMetrics();
    	Log.d("debug", "screenWidth = "+displayMetrics2.widthPixels+"|screenHeight = "+displayMetrics2.heightPixels);
    	
    	// 3.
    	Display display = getWindowManager().getDefaultDisplay();
    	Log.d("debug", "screenWidth = "+display.getWidth()+"|screenHeight = "+display.getHeight());
    	
    }

 

 

你可能感兴趣的:(android)