Two methods for Android to get the width and height pixel values of the device screen

private void get1() {  
    Resources resources = this.getResources();  
    DisplayMetrics dm = resources.getDisplayMetrics();  
    int width = dm.widthPixels;  
    int height = dm.heightPixels;  
    Log.d("方法1", width + " , " + height);  
}  
  
private void get2() {  
    WindowManager manager = this.getWindowManager();  
    DisplayMetrics outMetrics = new DisplayMetrics();  
    manager.getDefaultDisplay().getMetrics(outMetrics);  
    int width = outMetrics.widthPixels;  
    int height = outMetrics.heightPixels;  
    Log.d("方法2", width + " , " + height);  
}  

  The output is the same:

  1. 04-20 10:11:41.513 27052-27052/zhangphil.test D/method 1: 1080 , 2040  
  2. 04-20 10:11:41.514 27052-27052/zhangphil.test D/method 2: 1080 , 2040 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886622&siteId=291194637