Android Question Convert x,y value On Panel_Touch to Centimeter (CM)

tamba sylvester

Member
Licensed User
Longtime User
Hello need help on how to convert the x,y to cm

Panel Touch Event:
Private Sub panel_Touch (Action As Int, X As Float, Y As Float)

    Log(x&","&y)
'Need this x and Y to be in CM

    'Log(y)
    'Log(x)
End Sub
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
Your question is not entirely clear to me, but the layout can be represented as follows:
Literal string display:
log($"${x} cm, ${y} cm"$)
 
Upvote 0

Johan Schoeman

Expert
Licensed User
Longtime User
From Dots Per Inch....?
 
Upvote 0

zed

Active Member
Licensed User
The measurement is in DP.
Density-independent pixels: an abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1 dp is roughly equal to 1 px. When running on a higher density screen, the number of pixels used to draw 1 dp is scaled up by a factor appropriate for the screen's dpi.

Likewise, when on a lower-density screen, the number of pixels used for 1 dp is scaled down. The ratio of dps to pixels changes with the screen density, but not necessarily in direct proportion. Using dp units instead of px units is a solution to making the view dimensions in your layout resize properly for different screen densities. It provides consistency for the real-world sizes of your UI elements across different devices.
 
Upvote 0

emexes

Expert
Licensed User
Hello need help on how to convert the x,y to cm

Give this a burl:

Panel Touch Event:
Private Sub panel_Touch (Action As Int, X As Float, Y As Float)
	Dim DPI As Float = 160dip
	
	Dim Xinches As Float = X / DPI
	Dim Yinches As Float = Y / DPI
	
	Dim Xcentimetres As Float = Xinches * 2.54
	Dim Ycentimetres As Float = Yinches * 2.54
	
	Log("x, y = " & X & ", " & Y)
	Log("x, y = " & NumberFormat2(Xinches, 1, 2, 2, False) & """, " & NumberFormat2(Yinches, 1, 2, 2, False) & """")
	Log("x, y = " & NumberFormat2(Xcentimetres, 1, 1, 1, False) & " cm, " & NumberFormat2(Ycentimetres, 1, 1, 1, False) & " cm")
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
To get the ral size you should use this code which gives you th real dots per inch values:

Attached a small test program showing a ruler.

B4X:
Sub ExactSize
    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")
End Sub
 

Attachments

  • TestRuler.zip
    9.9 KB · Views: 124
Upvote 0

kimstudio

Active Member
Licensed User
Longtime User
Just curious, these dpi values are stored in display hardware by display manufactories or in OS ROM somewhere put by mobile phone makers? Are these values obtained by these API reliable on every phone?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…