Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Private ImageView1 As ImageView
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")
Dim obj As Reflector
obj.Target = ImageView1
obj.SetOnTouchListener("ImageView1_Touch")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
'Multi Touch
Dim jo As JavaObject = motionevent
Dim pc As Int = jo.RunMethod("getPointerCount", Null)
For p=0 To pc-1
Log(jo.RunMethod("getX", Array As Object(p)))
Log(jo.RunMethod("getY", Array As Object(p)))
Next
Return True
End Sub
Log(jo.RunMethod("getX", Array As Object(0)))
Log(jo.RunMethod("getY", Array As Object(0)))
Star-Dust
In principle it is true what says @DonManfred, it is better to use a panel and set the BackgroundImage at the bottom of the panel.
However if you want to know the click point of the image you can do so.
B4X:Sub Globals 'These global variables will be redeclared each time the activity is created. 'These variables can only be accessed from this module. Private ImageView1 As ImageView 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") Dim obj As Reflector obj.Target = ImageView1 obj.SetOnTouchListener("ImageView1_Touch") End Sub Sub Activity_Resume End Sub Sub Activity_Pause (UserClosed As Boolean) End Sub Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean 'Multi Touch Dim jo As JavaObject = motionevent Dim pc As Int = jo.RunMethod("getPointerCount", Null) For p=0 To pc-1 Log(jo.RunMethod("getX", Array As Object(p))) Log(jo.RunMethod("getY", Array As Object(p))) Next Return True End Sub
If you want to know only the first point touched remove the for and use this:
B4X:Log(jo.RunMethod("getX", Array As Object(0))) Log(jo.RunMethod("getY", Array As Object(0)))
Now I'm out of the office and I can not give you an example.Have you an example with GetColor() please
Now I'm out of the office and I can not give you an example.
But using the XUI library is by taking a snapshot of the panel/imageview you can get the bitmap and the point.
The same using the canvas.
But the best solution is have a bitmap with the same image that you inserted into panel/imageview, and with the bitmap variable then have the color of whatever on the x and y coordinates.
Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
Dim IV As ImageView = Sender
Dim Can As Canvas
Dim jo As JavaObject = motionevent
Dim X1 As Int = jo.RunMethod("getX", Array As Object(0))
Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0))
Can.Initialize(IV)
Dim Color As Int = Can.Bitmap.GetPixel(X1,Y1)
Log("This colo is touched:" & Color)
Return True
End Sub
Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
Dim IV As ImageView = Sender
Dim Can As Canvas
Dim jo As JavaObject = motionevent
Dim X1 As Int = jo.RunMethod("getX", Array As Object(0))
Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0))
Dim color As Int = BitmapImage.GetPixel(X1*(BitmapImage.Width/IV.Width),Y1*(BitmapImage.Height/IV.Height))
Return True
End Sub
With the 1st method sometime my app crash :/this is on method
B4X:Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean Dim IV As ImageView = Sender Dim Can As Canvas Dim jo As JavaObject = motionevent Dim X1 As Int = jo.RunMethod("getX", Array As Object(0)) Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0)) Can.Initialize(IV) Dim Color As Int = Can.Bitmap.GetPixel(X1,Y1) Log("This colo is touched:" & Color) Return True End Sub
But if you already have the image saved in a bitmap, do so before
B4X:Sub ImageView1_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean Dim IV As ImageView = Sender Dim Can As Canvas Dim jo As JavaObject = motionevent Dim X1 As Int = jo.RunMethod("getX", Array As Object(0)) Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0)) Dim color As Int = BitmapImage.GetPixel(X1*(BitmapImage.Width/IV.Width),Y1*(BitmapImage.Height/IV.Height)) Return True End Sub
Just can you answer me about 1 thing: My picture is 320x320 but when I get the position x & y the value in the right bottom corner is superior than 320 why ??make u debug and see the x and y positions if they are correct and internal to the view.
You have to do the rest![]()
Sub img_body_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
Dim IV As ImageView = Sender
Dim Can As Canvas
Dim jo As JavaObject = motionevent
Dim X1 As Int = jo.RunMethod("getX", Array As Object(0))
Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0))
X1 = X1 * (bitmap1.Width/(IV.Width))
Y1 = Y1 * (bitmap1.Height/(IV.Height))
'Log("test: " & IV.Width & " : " & IV.Height)
Log("X1= " & X1 & " Y1= " & Y1)
' 'Can.Initialize(IV)
If Y1 <= bitmap1.Height Then
Dim Color As Int = bitmap1.GetPixel(X1*(bitmap1.Width/IV.Width),Y1*(bitmap1.Height/IV.Height))
Log("color: " & Color)
If Color = -43174 Then
ToastMessageShow("ROUGE",False)
Else if Color = -9699497 Then
ToastMessageShow("VERT",False)
Else if Color = -11021569 Then
ToastMessageShow("BLEU",False)
Else if Color = -149161 Then
ToastMessageShow("JAUNE",False)
End If
End If
Return True
End Sub
Sub img_body_Touch(viewtag As Object, action As Int, X As Float, Y As Float, motionevent As Object) As Boolean
Dim IV As ImageView = Sender
Dim Can As Canvas
Dim jo As JavaObject = motionevent
Dim X1 As Int = jo.RunMethod("getX", Array As Object(0))
Dim Y1 As Int = jo.RunMethod("getY", Array As Object(0))
' X1 = X1 * (bitmap1.Width/(IV.Width))
' Y1 = Y1 * (bitmap1.Height/(IV.Height))
'Log("test: " & IV.Width & " : " & IV.Height)
Log("X1= " & X1 & " Y1= " & Y1)
' 'Can.Initialize(IV)
If Y1 <= bitmap1.Height Then
Dim Color As Int = bitmap1.GetPixel(X1*(bitmap1.Width/IV.Width),Y1*(bitmap1.Height/IV.Height))
Log("color: " & Color)
If Color = colors.red Then
ToastMessageShow("ROUGE",False)
Else if Color = colors.greenThen
ToastMessageShow("VERT",False)
Else if Color = -colors.blueThen
ToastMessageShow("BLEU",False)
Else if Color = -colors.yellowThen
ToastMessageShow("JAUNE",False)
End If
End If
Return True
End Sub