Android Question Click image

Hirogens

Active Member
Licensed User
Longtime User
Hello, other question: can I get the position of where I click on Imageview ?
 

DonManfred

Expert
Licensed User
Longtime User
No.
You can place a transparent panel above the imageview and handle the touchevents on the panel to find out the position.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
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)))
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
Because I try to use
B4X:
Panel1_Touch (Action As Int, X As Float, Y As Float)
and when I clik at the same place on 2 differents ecran size, I have 2 differentes positions :/
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
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)))

Have you an example with GetColor() please
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
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.
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
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.

Okay, because I have a picture with a body. On this picture you have 4 differents colors, each color is a muscle and I want to load different layout when I click on a muscle.
So you say with a bitmap I can do something like that ?
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
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
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
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
With the 1st method sometime my app crash :/
And the second work but the value in color is special: ex: red => -43174
Can you explain why ?

edit: the second give me this message :
java.lang.IllegalArgumentException: y must be < bitmap.height()
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
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;)
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
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;)
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 ??
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
The positions that you return are of View. You have to change them in proportion to the image, as I did in the second example
 
Upvote 0

Hirogens

Active Member
Licensed User
Longtime User
B4X:
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
this is my code, I try a lot of things but nothing work without problem :/
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
Double the proportion with the view, just once.
Use the Colors class to identify colors instead of numbers.
If they are particular colors you can use Colors.ARGB and Colors.RGB

B4X:
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
 
Upvote 0
Top