How does density value calculate in android

Density can be calculated by the following formula:

Density = sqrt((wp * wp) + (hp * hp)) / di

Where:

wp is width resolution in pixels,

hp is height resolution in pixels, and

di is diagonal size in inches.


int pixel = 120;
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (pixel* scale + 0.5f);

Refer this following links

  • getDisplayMetrics(), for current display metrics
  • density, This is a scaling factor for the Density Independent Pixel unit

The formula is actual-dpi / 160. (Everything is scaled to 160 dpi.)

Tags:

Android