B4J Question How to drag a control to move it to another position

xulihang

Active Member
Licensed User
Hi there,

I am making a simple layered image editor. Its major function is to add text layers on images. I am trying to use Label as text layers and use snapshot to export merged images.

I try to use the mouse to drag a control to a new place.

I use the mousedragged event, but the dragging process is shaky.

B4X:
Sub ImageView1_MouseDragged (EventData As MouseEvent)
    Dim node As Node
    node=Sender
    node.Left=EventData.X
    node.Top=EventData.Y
    node.MouseCursor=fx.Cursors.HAND
End Sub
 
Last edited:

xulihang

Active Member
Licensed User
The log output shows the position changes. This is why the control will shake. I don't know the reason.

B4X:
Log("Dragged")
Log(EventData.X)
Log(EventData.Y)
B4X:
Dragged
48
89
Dragged
348
106
Dragged
46
91
Dragged
348
107
Dragged
45
94
Dragged
348
108
Dragged
45
96
Dragged
347
111
Dragged
45
97
Dragged
347
113
release
45
97
 

Erel

Administrator
Staff member
Licensed User
Your code is incorrect because X and Y are relative to the node position.

B4X:
Sub Process_Globals
   Private fx As JFX
   Private MainForm As Form
   Type PositionData (PressedX As Int, PressedY As Int)
   Private Button1 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
   MainForm = Form1
   MainForm.RootPane.LoadLayout("1") 'Load the layout file.
   MainForm.Show
End Sub

Sub Button1_MouseDragged (EventData As MouseEvent)
   Dim view As B4XView = Sender
   Dim pd As PositionData = view.Tag
   Dim ParentX As Int = EventData.X + view.Left
   Dim ParentY As Int = EventData.Y + view.Top
   view.Left = ParentX - pd.PressedX
   view.Top = ParentY - pd.PressedY
End Sub

Sub Button1_MousePressed (EventData As MouseEvent)
   Dim view As B4XView = Sender
   Dim pd As PositionData
   pd.PressedX = EventData.X
   pd.PressedY = EventData.Y
   view.Tag = pd
End Sub
 
Top