Reputation: 7916
Can anybody explain me what formula android use to calculate screen density?
Upvotes: 16
Views: 22379
Reputation: 2277
To calculate screen density, you can use this equation:
Screen density = Screen width (or height) in pixels / Screen width (or height) in inches
Upvotes: 0
Reputation: 426
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.
Upvotes: 41
Reputation: 34833
int pixel = 120;
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (pixel* scale + 0.5f);
Refer this following links
Upvotes: 1
Reputation: 234857
The formula is actual-dpi / 160. (Everything is scaled to 160 dpi.)
Upvotes: 6