How to draw circles in real sizes mm

barx

Well-Known Member
Licensed User
Longtime User
Hi

I'm using a canvas object to draw circles but I would like the circles to show an exact real size on all devices. i.e. on screen measured diameter = 50mm on all.

i've tied using an online calculator and converting mm to pixels and the using that for radius of circle but no joy, any ideas?

thanks
 

ukimiku

Active Member
Licensed User
Longtime User
Have you tried providing your circle diameter/radius in "pid" units? I am new to Basic4Android, but I remember reading somewhere in the tutorials that units given in "pid" are supposed to refer to the actual physical size of graphical objects (except text size which supposedly already is given in real-life physical size).

Maybe this helps?

Regards,
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can use the reflection library to get the current display metrics for the device. It contains fields that hold the exact dpi for the X and Y axes.

This code should do it:

B4X:
Dim Xdpi,Ydpi As Double
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    Xdpi=r.GetField("xdpi")
    Ydpi=r.GetField("ydpi")

More info here.
 
Last edited:
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
OK, using the above info Icame up with this sub

B4X:
Sub mmTopx(mm As Float) As Float
Dim Xdpi,Ydpi As Double
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    Xdpi=r.GetField("xdpi")
    'Ydpi=r.GetField("ydpi")
   
   Return (mm / 25.4) * Xdpi
End Sub

but it doesn't do as I need, it's still different sizes on different screens and none are the size I hoped for, all smaller.

I worked on the principle that DPI is dots per INCH, so divided my mm number by 25.4 to convert to inches, then multiplied that by the dpi.

Can anybody see where I'm going wrong and how to rectify, thanks
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Thanks Klaus,

I been tinkering some more and have more details. My test devices are:

Xperia X10 Mini Pro - Density 0.75
HTC Desire Z - Density 1.5
Archos G9 101 - Density 1.0

With the above code the 1.0 and 1.5 show right but the 0.75 is wrong.

So thought it may be a density issue so used

B4X:
Return ((mm / 25.4) * Xdpi) / Density

now the 0.75 and 1.0 are right but the 1.5 is wrong.

mind boggling.
 
Upvote 0

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
I can't add any great insight to this discussion, but I don't understand how "Density" can be used in the calculation because it is not a fixed value but indicates a range of actual DPIs, as explained here and on this page which says: Each generalized size and density spans a range of actual screen sizes and densities. For example, two devices that both report a screen size of normal might have actual screen sizes and aspect ratios that are slightly different when measured by hand.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
It may have no significance, I'm just trying things to make it work....
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
For me, density couldn't be a concern !
What Xdpi values do you get for the three devices ? Are these values realistic ?
With the physical size of the screen and the Xdip values you could calculate if these Xdpi values are realistic or not.
But making calculations with the density value is not realistic.

Best regards.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It seems to return accurate info on my HTC Sensation.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Maybe it's just this x10 pro mini then acting silly begger.

xdpi values:

x10 mini pro - 120
Desire Z - 254
G9101 - 149.82489013671875 LOL

Looking at gsmarena it says x10 mini pro should be 157 ppi is that the same as dpi?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
It seems that the system is returning wrong values !
The screen size is 2.5'' diagonal.
The pixel numbers are 240 / 320 a 3 / 4 ratio.
So the physical screen size is 1.5'' width and 2'' height.
This gives 160 dpis and not 120 which are the Android standardized dpis or densityDpis.
What value do you get with densityDpi = r.GetField("densityDpi") ?
This value should be 120, but Xdpi and Ydpi should be 160 !

Best regards.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
It seems that the system is returning wrong values !
The screen size is 2.5'' diagonal.
The pixel numbers are 240 / 320 a 3 / 4 ratio.
So the physical screen size is 1.5'' width and 2'' height.
This gives 160 dpis and not 120 which are the Android standardized dpis or densityDpis.
What value do you get with densityDpi = r.GetField("densityDpi") ?
This value should be 120, but Xdpi and Ydpi should be 160 !

Best regards.

Both return 120 :confused:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I'm afraid that there is no solution.
It's estonishing that the manufacturer sets wrong information in the system.
Even dividing by density, which in this case gives the right value, is not a solution because it's valid only for this particular case.
A solution could be if you could get the physical sceen size and with the pixel numbers it would be possible to calculate the dips.
Unfortunately I don't know if the physical sceen size is stored somewhere in the system.

Best regards.
 
Upvote 0

barx

Well-Known Member
Licensed User
Longtime User
Ok thanks klaus, think I will just not cater for this size device. The small screens are a dying breed anyway. Maybe will try it on a wildfire see what happens.

Now to find someone with a wildfire. Lol

Sent from my HTC Desire Z
 
Upvote 0

canalrun

Well-Known Member
Licensed User
Longtime User
Ok thanks klaus, think I will just not cater for this size device. The small screens are a dying breed anyway. Maybe will try it on a wildfire see what happens.

Now to find someone with a wildfire. Lol

Sent from my HTC Desire Z

This can be a problem on all computers, not just Android devices.

It's kludgy, but what I have done in the past when accuracy was important, is draw a rectangle on screen in a setup area. I then tell the user to resize the rectangle (drag corners) until it is a certain width and height. I can then calculate the vertical and horizontal resolution.

Barry.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
@nfordbscndrd
All these links point to DisplayMetrics.
But as we don't know if the data given back is correct or not depending on the device there is no general solution with DisplayMetrics.

@barx
I'm afraid that the most reliable solution is canalruns' solution.

Best regards.
 
Upvote 0
Top