getting the screen density programmatically in android?

后端 未结 19 2656
感动是毒
感动是毒 2020-11-22 01:56

How to get the screen density programmatically in android?

I mean: How to find the screen dpi of the current device?

19条回答
  •  感情败类
    2020-11-22 02:23

    This should help on your activity ...

    void printSecreenInfo(){
    
        Display display = getWindowManager().getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);
    
        Log.i(TAG, "density :" +  metrics.density);
    
        // density interms of dpi
        Log.i(TAG, "D density :" +  metrics.densityDpi);
    
        // horizontal pixel resolution
        Log.i(TAG, "width pix :" +  metrics.widthPixels);
    
         // actual horizontal dpi
        Log.i(TAG, "xdpi :" +  metrics.xdpi);
    
        // actual vertical dpi
        Log.i(TAG, "ydpi :" +  metrics.ydpi);
    
    }
    

    OUTPUT :

    I/test( 1044): density :1.0
    
    I/test( 1044): D density :160
    
    I/test( 1044): width pix :800
    
    I/test( 1044): xdpi :160.0
    
    I/test( 1044): ydpi :160.42105
    

提交回复
热议问题