Android Question Detect User Stopped Dragging

mmieher

Active Member
Licensed User
Longtime User
New to GestureDetector ....

Using GD to drag panels around. How can I tell when the user has lifted their finger and the panel is in the final desired location?

Marc
 

mmieher

Active Member
Licensed User
Longtime User
Thank you, Erel. I learned a lot by studying the DraggableView class!

Here is my solution in case it helps someone else.

The idea here is to be able to drag a Panel with a BackgroundImage from a parent panel to a target panel "bullseye". At that point, process the original panel (add to file list, etc).

Marc

B4X:
*** Main ***

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim PicPanel As Panel
    Dim GDPanel(10) As Panel    '    panels of pictures or documents
    Dim dv(10) As PicturePicker
    Dim TargetPanel As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
  
    Activity.LoadLayout("CustComp3")
  
    GDPanel(0).Initialize("GDPanel")
    GDPanel(0).Tag = "0"
    PicPanel.AddView(GDPanel(0),10dip,10dip,140dip,135dip)
    GDPanel(0).SetBackgroundImage(LoadBitmapResize(File.DirAssets,"CashCow.jpg",140dip,135dip,False))
    dv(0).Initialize(Activity,GDPanel(0),TargetPanel)
  
    GDPanel(1).Initialize("GDPanel")
    GDPanel(1).Tag = "1"
    PicPanel.AddView(GDPanel(1),210dip,10dip,140dip,135dip)
    GDPanel(1).SetBackgroundImage(LoadBitmapResize(File.DirAssets,"s-touchme.png",140dip,135dip,False))
    dv(1).Initialize(Activity,GDPanel(1),TargetPanel)
  
End Sub

Sub PictureSelected(inTag As String)
  
    Log("PictureSelected = " & inTag)
  
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

*** PicturePicker Class ***

Sub Class_Globals
  
    Private innerView As View
    Private downx, downy As Int
    Private ACTION_DOWN, ACTION_MOVE, ACTION_UP As Int
  
    Private sourcePanel As Panel
    Private targetPanel As Panel
    Private saveTop As Int
    Private saveLeft As Int
  
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(Activity As Activity,v As View, tp As Panel)
  
    innerView = v
    targetPanel = tp
    saveTop = innerView.Top
    saveLeft = innerView.Left
  
    sourcePanel.Initialize("")
    sourcePanel.Tag = v.tag
    sourcePanel.Color = Colors.Transparent
    Activity.AddView(sourcePanel, v.Left, v.Top, v.Width, v.Height)
    ACTION_DOWN = Activity.ACTION_DOWN
    ACTION_MOVE = Activity.ACTION_MOVE
    ACTION_UP = Activity.ACTION_UP
    Dim r As Reflector
    r.Target = sourcePanel
    r.SetOnTouchListener("sourcePanel_Touch")

End Sub

Private Sub sourcePanel_Touch (o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
  
    Dim fHitTarget As Boolean
  
    Dim xCenter As Int = sourcePanel.Left + (sourcePanel.Width / 2)
    Dim yCenter As Int = sourcePanel.Top + (sourcePanel.Height / 2)

    If xCenter > targetPanel.Left And xCenter < (targetPanel.Left + targetPanel.Width) And _
       yCenter > targetPanel.Top And xCenter < (targetPanel.top + targetPanel.Height) Then
    '    Log("HIT!")
        fHitTarget = True
    Else
        fHitTarget = False
    End If
  
    '    Log("ACTION = " & ACTION)
  
    If ACTION <> ACTION_UP Then
        innerView.Left = innerView.Left + x - downx
        innerView.Top = innerView.Top + y - downy
        sourcePanel.Left = innerView.Left
        sourcePanel.Top = innerView.Top
    Else
    '    Log("FINGER UP!")
        If fHitTarget <> True Then
            sourcePanel.Left = saveLeft
            sourcePanel.Top = saveTop
            innerView.Left = saveLeft
            innerView.Top = saveTop
        Else
            CallSub2(Main,"PictureSelected",sourcePanel.Tag)
        End If
    End If
  
    If ACTION = ACTION_DOWN Then
        downx = x
        downy = y
        Log("Wow.  Got an ACTION_DOWN")
    End If
  
    Return True
  
End Sub
 
Upvote 0
Top