16

Can anybody explain me what formula android use to calculate screen density?

Dharman
  • 30,962
  • 25
  • 85
  • 135
teoREtik
  • 7,886
  • 15
  • 46
  • 65

4 Answers4

40

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.

C0deH4cker
  • 3,959
  • 1
  • 24
  • 35
Hari Kumar
  • 416
  • 4
  • 2
6

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

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1
int pixel = 120;
final float scale = getResources().getDisplayMetrics().density;
int dip = (int) (pixel* scale + 0.5f);

Refer this following links

Labeeb Panampullan
  • 34,521
  • 28
  • 94
  • 112
  • Thanks a lot, but i have to repeat my question again. I want to know how does OS Android calculate density. In your code snippet it is scale value. – teoREtik Feb 09 '11 at 08:45
  • I read a documentation about density, but I didn`t understand it completely. So I want to know, how does it calculate. That`s why I established this question – teoREtik Feb 09 '11 at 09:23
  • 2
    I think you need to rephrase your question. Explain a little more what you want to accomplish. Android's concept of density is the ratio of actual number of pixels/inch to the default pixel density of 160 pixels per inch. Take a look at the [DisplayMetrics](http://developer.android.com/reference/android/util/DisplayMetrics.html) class docs. – Ted Hopp Feb 10 '11 at 04:39
0

To calculate screen density, you can use this equation:

Screen density = Screen width (or height) in pixels / Screen width (or height) in inches

sandes
  • 1,917
  • 17
  • 28