2

I have defined a circle radius dimension in a resources file like so:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="circleRadius">15dp</dimen>
</resources>

And then I draw the circle in a custom View like this:

Resources res = getResources();
float radius = res.getDimension(R.dimen.circleRadius);

...

canvas.drawCircle(randomX, randomY, radius, paint);

I was under the impression that this would produce a circle of the same physical size on any device, because the units are specified in dp, but it doesn't. See the screenshots below.

Device 1 (skin=WVGA800, density=240):

Device 1

Device 2 (skin=QVGA, density=120):

Device 2

Device 3 (skin=1024x768, density=160):

Device 3

For each device, I ticked the Scale display to real size option when launching, and used the same settings (screen size=3.7 in, monitor dpi=105). Is this where I've gone wrong? Is there something I am not understanding?

XåpplI'-I0llwlg'I -
  • 21,649
  • 28
  • 102
  • 151

2 Answers2

3

Use:

//get dots per inch

int dpi = getApplicationContext().getResources().getDisplayMetrics().densityDpi;

//the physical size you want in inches

float x = 0.25f;

//get number of dots for that physical size

final float radius = x*(new Float(dpi));

//now you use radius in drawCircle() method
joyx
  • 77
  • 4
2

You just got wrong impression. To compare the way you do, you need to normalize the output. Grab all the screenshots and scale them to the same density and resolution. Then compare again.

EDIT

If you want to show 1 inch diameter circle, despite of the hardware you should not be using dp but in or pt unit. See docs: http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Then how does one draw a circle of the same physical size on any device? – XåpplI'-I0llwlg'I - Nov 19 '12 at 11:59
  • Screen **size** differs, in physical size and screen size. You will never get exactly the same if you try to measure with ruler. What you want to accomplish exactly? – Marcin Orlowski Nov 19 '12 at 12:01
  • I want to draw a circle that is the same physical size (e.g. 1 inch radius) on any device. Similar to [this example](http://developer.android.com/guide/practices/screens_support.html#density-independence). – XåpplI'-I0llwlg'I - Nov 19 '12 at 12:08
  • Ok, I tried with `pt`, but the circles are still different sizes on the different devices... Am I supposed to leave the `Scale display to real size` option unticked? – XåpplI'-I0llwlg'I - Nov 19 '12 at 12:20