Get the phone pixel, density and width (dp)

//                 Get the phone pixel, density and width and height ( dp )
                 WindowManager wm = (WindowManager) MainActivity. this .getSystemService(Context. WINDOW_SERVICE ) ;
                 DisplayMetrics dm = new DisplayMetrics() ;
                 wm.getDefaultDisplay().getMetrics(dm) ;
                 int width = dm.widthPixels ;          // screen width (pixels)
 int height = dm.heightPixels ;        // screen height (pixels)
 float density = dm.density ;          // screen density ( 0.75 / 1.0 / 1.5 )
                                                int densityDpi = dm. densityDpi ;      // screen density dpi ( 120 / 160 / 240 )
 // screen width algorithm : screen width (pixels) / screen density
 int screenWidth = ( int ) (width / density) ;   // screen width ( dp)
 int screenHeight = ( int ) (height / density) ; // screen height (dp)
 Log. e ( "h_bl" , " screen width (pixels): " + width) ;
 Log. e ( "h_bl"                                                                                , " screen height (pixels): " + height) ;
                 Log. e ( "h_bl" , " screen density ( 0.75 / 1.0 / 1.5 ): " + density) ;
                 Log. e ( "h_bl" , " screen density dpi ( 120 / 160 / 240 ): " + densityDpi) ;
                 Log. e ( "h_bl" , " Screen Width ( dp ): " + screenWidth) ;
                 Log. e ( "h_bl" ," Screen height (dp):" + screenHeight);

Guess you like

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