views:

389

answers:

2

I need to know exactly how big the screen is on the device in real units of length so I can calculate the acceleration due to gravity in pixels per millisecond.

Is there a method somewhere in the Android API for this?

A: 

You need to use the screen density to calculate this.

Context.getResources().getDisplayMetrics().density

According to the documentation:

The logical density of the display. This is a scaling factor for the Density Independent Pixel unit, where one DIP is one pixel on an approximately 160 dpi screen (for example a 240x320, 1.5"x2" screen), providing the baseline of the system's display. Thus on a 160dpi screen this density value will be 1; on a 120 dpi screen it would be .75; etc.

This value does not exactly follow the real screen size (as given by xdpi and ydpi, but rather is used to scale the size of the overall UI in steps based on gross changes in the display dpi. For example, a 240x320 screen will have a density of 1 even if its width is 1.8", 1.3", etc. However, if the screen resolution is increased to 320x480 but the screen size remained 1.5"x2" then the density would be increased (probably to 1.5).

CaseyB
Use xdpi and ydpi as described by the other poster. The density is deliberately not an absolute mapping to the physical screen size, but a more abstract unit that can be quantized to a small number of discreet values (currently 120dpi, 160dpi, 240dpi).
hackbod
+1  A: 

android developers screen info.

use xdpi * widthPixels and ydpi * heightPixels might get you what you want i think.

runrunraygun