Italian Intercettare x and y del dito su un object

ivanomonti

Expert
Licensed User
Longtime User
Come da titolo, vorrei intercettare la posizione della pressione del dito su un determinato object (panel o canvas) come devo comportarmi, cosa devo usare per ottenere questo controllo.
 

ivanomonti

Expert
Licensed User
Longtime User

mmmm non e proprio quello che cerco, l'esempio e al click, io devo creare un movimento tipo lo scroll ma non voglio lo scrool ma ad esempio leggere il delta y e la posizione x

esempio

B4X:
if x = 10 and <= 200 then
   if deltaY.value > 0 then
     fai
   else
     fai
   end if
end if

Spero di essermi fatto capire.
 

ivanomonti

Expert
Licensed User
Longtime User
Qualcosa non va

Ciao Ragazzi della ML;

Questo codice sembra andare quasi bene, ma funziona solo se muovo il dito in orizzontale e non verticale

B4X:
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
    Select Action
        Case Activity.ACTION_DOWN
            LastY = Y
      Case Activity.ACTION_MOVE
         If Abs(Y - LastY) > 0 Then
            index = index + 1
            If index > value1.Length-1 Then
               index = 0
            End If
         Else
            index = index-1
            If index < 0 Then
               index = 0
            End If
         End If
         scrollValue
      Case Activity.ACTION_UP
    End Select
    Return True        
End Sub

Dove sbaglio!!!! :BangHead:
 

arenaluigi

Well-Known Member
Licensed User
Longtime User
Ciao Ragazzi della ML;

Questo codice sembra andare quasi bene, ma funziona solo se muovo il dito in orizzontale e non verticale

B4X:
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
    Select Action
        Case Activity.ACTION_DOWN
            LastY = Y
      Case Activity.ACTION_MOVE
         If Abs(Y - LastY) > 0 Then
            index = index + 1
            If index > value1.Length-1 Then
               index = 0
            End If
         Else
            index = index-1
            If index < 0 Then
               index = 0
            End If
         End If
         scrollValue
      Case Activity.ACTION_UP
    End Select
    Return True        
End Sub

Dove sbaglio!!!! :BangHead:

Ma la variabile X che valori ti da ?
 

ivanomonti

Expert
Licensed User
Longtime User
Ecco cosa voglio

Come detto altro giorno vorrei creare delle classi GUI, questa e la prima, ma mancano ancora due cose e del betatest

datapicker_for_android.png


Ciò che non capisco e perchè il codice Activity.ACTION_MOVE si mette in moto solo se muovo il dito in orizzontale non verticate tipo

Top to Botton or Botton to Top :BangHead:

B4X:
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean 'Return True to consume the event
    Select Action
        Case Activity.ACTION_DOWN
            LastY = Y
        Case Activity.ACTION_MOVE
            If Abs(Y - LastY) > 0 Then
                index = index + 1
                If index > value1.Length-1 Then
                    index = 0
                End If
            Else
                index = index-1
                If index < 0 Then
                    index = 0
                End If
            End If
            scrollValue
        Case Activity.ACTION_UP
    End Select
    Return True        
End Sub


Chiedo aiuto per capire questo mio dilemma, che forse troppo concentrato a cercare che non trovo la soluzione che sta sotto il naso.
 

Dominex

Active Member
Licensed User
Longtime User
C'è una cosa che non ho capito , ti servono le coordinate xy esatte in cui punti il dito sul panel e durante il movimento, oppure altro?

Con questo codice le coordinate ti vengono date su entrambe le coordinate.

B4X:
Sub Globals
   Dim Panel1 As Panel
   Dim Label1 As Label
   Dim X0,Y0,X1,Y1 As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Panel1.Initialize("Panel1")
   Activity.AddView(Panel1,0,0,100%x,100%y)
   Label1.Initialize("")
   Activity.AddView(Label1,0,0,100%x,10%y)
End Sub

Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean
   Select Action
      Case Activity.ACTION_DOWN
           X0 = X
         Y0 = Y
         Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
      Case Activity.ACTION_MOVE
         X1 = X-X0
         Y1 = Y-Y0
         Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
     Case Activity.ACTION_UP
          Label1.Text=""
    End Select       
End Sub
 
Last edited:

ivanomonti

Expert
Licensed User
Longtime User
C'è una cosa che non ho capito , ti servono le coordinate xy esatte in cui punti il dito sul panel e durante il movimento, oppure altro?

Con questo codice le coordinate ti vengono date su entrambe le coordinate.

B4X:
Sub Globals
   Dim Panel1 As Panel
   Dim Label1 As Label
   Dim X0,Y0,X1,Y1 As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Panel1.Initialize("Panel1")
   Activity.AddView(Panel1,0,0,100%x,100%y)
   Label1.Initialize("")
   Activity.AddView(Label1,0,0,100%x,10%y)
End Sub

Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean
   Select Action
      Case Activity.ACTION_DOWN
           X0 = X
         Y0 = Y
         Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
      Case Activity.ACTION_MOVE
         X1 = X-X0
         Y1 = Y-Y0
         Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
     Case Activity.ACTION_UP
          Label1.Text=""
    End Select       
End Sub


Le coordinate arrivano solo se il dito striscia in orizzontale e non in verticale, faccio esempio se ho il telefono in verticale e tiro il dito dall'alto al basso o viceversa non scatta evento, se lo faccio da dx a sx o viceversa scatta l'evento Move.

Lo scopo mi serve fare girare il tamburo con i dati su e giù come in una listbox nulla di più.

Ciao
 

ivanomonti

Expert
Licensed User
Longtime User
Il mio esempio funzione in ogni direzione, se all'interno della tua app non funzione c'è un altro fattore che ignoro.

Quello che dice e vero, ho riscritto il tutto e sembra andare, mistero, ma ho trovato altra difficoltà, una volta eseguito il codice dentro alla sezione Touch come faccio a riprendere il click o longclick!!

Tuo codice

B4X:
Sub Panel1_Touch(Action As Int, X As Float, Y As Float) As Boolean
   Select Action
      Case Activity.ACTION_DOWN
            X0 = X
            Y0 = Y
            Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
      Case Activity.ACTION_MOVE
            X1 = X-X0
            Y1 = Y-Y0
                        ' modifica di ivano monti
         If Abs(Y1 - Y-Y0) > 40 Then
            index = index + 1
            If index > value1.Length-5 Then
               index = 0
            End If
         Else If Abs(Y1 - Y-Y0) < -40 Then
            index = index-1
            If index < 0 Then
               index = 0
            End If
         End If
         scrollValue
                        ' fine modifica di ivano monti
            Label1.Text="X0: "&X&" / Y0: "&Y&CRLF&"X1: "&X1&" / Y1: "&Y1
     Case Activity.ACTION_UP
             Label1.Text=""
    End Select
   Return True
End Sub

B4X:
Sub Panel1_LongClick
   Label1.Text = Result(2)
   Msgbox(Result(2),"Select item")
End Sub

Sub Panel1_Click
   Label1.Text = Result(2)
   Msgbox(Result(2),"Select item")
End Sub

Risultato finale

Listener.png
 
Last edited:

Dominex

Active Member
Licensed User
Longtime User
Devi crearti l'evento Click e LongClick all'interno dell'evento Touch rilevando via codice la parte dello ScrollView che stai premendo attraverso le coordinate xy. L'unica differenza e che per il LongClick devi tener conto del tempo trascorso dopo l'evento ACTION_DOWN per differenziarlo dal normale Click.
 

klaus

Expert
Licensed User
Longtime User
Hi ivanomonti,
You also asked a similar question in the english forum and there I asked you back what exactly you want to do but no answer there yet.
Depending on what you want to do the answer could be different.
Do you want to move something when you move the finger or do you want only have the information when you release the finger.

The code in post #15 will not work:
Y1 = Y - Y0
If Abs(Y1 - Y-Y0) > 40 Then

You have Y1 = Y - Y0 then you have Y1 - Y - Y0 so Y - Y0 - Y - Y0 = -2Y0
and Abs(-2Y0) = 2Y0

In the code in the english post:
If Abs (Y - LastY)> 0 Then
Abs (Y - LastY) will always be 0 or positive, because you take the absolute value !

Are you trying to make a wheel selector like the screenshot in post #15 ?
If yes, what kind of view are you using for the name display ?
Then you would probably add a kind of dynamic sliding.
If you want you could post a small project with what you have done and I could have a look at it.

In your code in the Touch event routine you should also remove Return True
and change this Sub Panel1_Touch (Action As Int, X As Float, y As Float) remove the As Boolean at the end.
It's no more needed this has changed since version 2.00

In standard Android if you use the Touch event this one consumes the Click and LongClick events. If you really need these you must code them yourself within the Touch event and a Timer as Dominex already suggested.

Best regards.
 
Top