Android Question Image touch position

Duncan H Williamson

Member
Licensed User
Longtime User
Hi all, I have a need to display an image full screen size, and save a touch point to a data table, the touch point will then be recalled at a later date and a small image superimposed over the original image at that point.
I have figured out how to get the x,y coordinates and save them and then use them to position the image
but I have a need to retrieve or create this coordinate on several different phones of differing screen sizes . can someone please help me with a way of scaling the coordinates , I am assuming I should be storing them as a percentage of total image size ....any suggestions would be greatly apreciated
 

eurojam

Well-Known Member
Licensed User
Longtime User
Hi,
if you work with a panel and a canvas, the touch coordinates will always be like absolute image coordinates in pixel - that is what I think....may be I am wrong, but test this code, it will give you absolute image coordinates....you can play with the position and size of the panel...

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Panel1 As Panel
    Dim Canvas1 As Canvas
    Dim b As Bitmap

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Panel1.Initialize("panel1")
    Activity.AddView(Panel1,0, 200dip,400dip,400dip)
   
    Canvas1.Initialize(Panel1)
   
    Dim r1, r2 As Rect
    b.Initialize(File.DirAssets, "test.png")
    r1.Initialize(0,0, b.Width, b.Height)
   
    Canvas1.DrawBitmap(b, Null, r1)   

End Sub

Sub panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action= 1 Then
        If x < b.Width And y < b.height Then
          Log("X " & X)
          Log("Y " & Y)
        End If
    End If 
End Sub
 
Upvote 0

Duncan H Williamson

Member
Licensed User
Longtime User
Hi,
if you work with a panel and a canvas, the touch coordinates will always be like absolute image coordinates in pixel - that is what I think....may be I am wrong, but test this code, it will give you absolute image coordinates....you can play with the position and size of the panel...

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim Panel1 As Panel
    Dim Canvas1 As Canvas
    Dim b As Bitmap

End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Panel1.Initialize("panel1")
    Activity.AddView(Panel1,0, 200dip,400dip,400dip)
  
    Canvas1.Initialize(Panel1)
  
    Dim r1, r2 As Rect
    b.Initialize(File.DirAssets, "test.png")
    r1.Initialize(0,0, b.Width, b.Height)
  
    Canvas1.DrawBitmap(b, Null, r1)  

End Sub

Sub panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action= 1 Then
        If x < b.Width And y < b.height Then
          Log("X " & X)
          Log("Y " & Y)
        End If
    End If
End Sub
Hi, thanks for your reply, I will try this today !!
Regards
Duncan
 
Upvote 0
Top