Reputation: 22404
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 2 (skin=QVGA, density=120):
Device 3 (skin=1024x768, density=160):
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?
Upvotes: 2
Views: 2271
Reputation: 77
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
Upvotes: 3
Reputation: 75636
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
Upvotes: 2