Method for obtaining pix value of device screen width and height by Android

  • 2021-09-05 01:00:01
  • OfStack

This article mainly introduces the Android device screen width height pix value method, this site feels quite good, now share with you, also give you a reference. Follow this site from 1 to have a look

Method 1:


WindowManager wm = (WindowManager) this 
.getSystemService(Context.WINDOW_SERVICE); 
int width = wm.getDefaultDisplay().getWidth(); 
int height = wm.getDefaultDisplay().getHeight(); 

Method 2:


WindowManager wm1 = this.getWindowManager(); 
int width1 = wm1.getDefaultDisplay().getWidth(); 
int height1 = wm1.getDefaultDisplay().getHeight(); 

Method 1 gets the screen width in a similar way to Method 2, except that the way to get the WindowManager object is different.

Method 3:


 WindowManager manager = this.getWindowManager(); 
DisplayMetrics outMetrics = new DisplayMetrics(); 
manager.getDefaultDisplay().getMetrics(outMetrics); 
int width2 = outMetrics.widthPixels; 
int height2 = outMetrics.heightPixels;

Method 4:


 Resources resources = this.getResources(); 
DisplayMetrics dm = resources.getDisplayMetrics(); 
float density1 = dm.density; 
int width3 = dm.widthPixels; 
int height3 = dm.heightPixels;

Method 3 is similar to Method 4.


Related articles: