Android Question How to draw a line specified in mm unit?

udg

Expert
Licensed User
Longtime User
Hi all,

using a Canvas on a Bitmap mutable, I'd like to draw a line that is 22.85mm long and 0.33mm thick.
Beforehand I should define an ImageView large enough to hold the desidered bitmap (let's say a 30x30mm ImageView) then precisely draw my line and have the bitmap as the imageview background.

My problem is that I don't understand how to exactly render that measures irrespective of devices densities and dimensions (in pixel).

Umberto
 

udg

Expert
Licensed User
Longtime User
Thanks Erel.
If I understand it correctly, using that code I get H/V densities of current device in dots per inch (and so I can calculate dots per mm).
Multiplying those values for the desidered measures I should end with the precise number of dots needed.
Is that number that I then pass to any function (like InitializeMutable, Drawline..)?

As an example (using inches):
B4X:
Dim r AsReflector
 r.Target = r.GetContext
 r.Target = r.RunMethod("getResources")
 r.Target = r.RunMethod("getDisplayMetrics")
xdpi=r.GetField("xdpi")                                        'is xdpi an int or a float?
ydpi=r.GetField("ydpi")

Dim imgarea As ImageView
imgarea.Initialize("")
Activity.AddView(imgarea,0dip,0dip,3*xdpi,3*ydpi)   '3x3 inches area??
Dim bm As Bitmap
bm.InitializeMutable(imgarea.Width,imgarea.Height)
Dim cv As Canvas
cv.Initialize2(bm)
cv.DrawLine(0,0,0,1.5*ydpi,Colors.White,1*xdpi)                   'vertical line - 1.5inch long - 1 inch thick??
cv.DrawLine(1*xdpi,0,1*xdpi,1.5*ydpi,Colors.White,1*xdpi)     'same as above but shifted right of 1 inch??

Is the above code correct?

Umberto
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Thank you so much.
 
Upvote 0
Top