Hi Richard,

On Jan 12, 6:54 pm, Richard Schilling <[email protected]>
wrote:
> Did you try fetching the scale that will allow you to convert from DPI
> to actual screen pixels?  I found this very reliable:
>
>      final scale =
> getContext().getResources().getDisplayMetrics().density;

The description of that says:

    "on a 160dpi screen this density value will be 1; on a 120 dpi
screen
    it would be .75; etc."

> Then multiply scale by one inch of pixels in DPI resolution (depending
> on the screen you're on) - 120, 160, 240, 320.
>
> This API call ...
>
>      int densityDpi =
> getContext().getResources().getDisplayMetrics().densityDpi;
>
> will tell you the density of the screen you're on.

Approximately.

> So, this will tell you how many screen pixels equate to an inch.

I'm not at all sure that it does.

>  Then
> you can use that to lay out your ruler markings:
>      int pxPerInch = 340; // default value - note it's a new density
> in API version 9.
>
>      switch(densityDpi){
>           case DisplayMetrics.DENSITY_LOW:
>                       pxPerInch = 120 * scale;
>                       break;
>           case DisplayMetrics.DENSITY_MEDIUM:
>                       pxPerInch = 160 * scale;
>                       break;
>           case DisplayMetrics.DENSITY_HIGH:
>                       pxPerInch = 240 * scale;
>                       break;
>      }

Note that the values of those constants DENSITY_* are their numeric
values, so there is no need for the case statement; you can just
write:

pxPerInch = densityDpi * scale;

However, I really don't think that does what you believe it does.  I
think that densityDpi reliably gives the approximate dpi, and density
just gives densityDpi/160.  Multiplying them together doesn't do
anything useful.  But I could be wrong!

Thanks,  Phil.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to