Android Question Draw straight line wit finger

samannnn

Member
How make line with finger? I want to draw line between to point. Line must be start from point 1 and with my finger connect it to point 2. It's improtant to draw straight line.
Same as line in this game:
Emoji Puzzle: Thank you for you helps.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Full example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
    Private cvs As B4XCanvas
    Private px, py As Int
End Sub

Public Sub Initialize
    
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    cvs.Initialize(Panel1)
End Sub


Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action = Panel1.TOUCH_ACTION_DOWN Then
        px = X
        py = Y
    Else
        cvs.ClearRect(cvs.TargetRect)
        cvs.DrawLine(px, py, X, Y, xui.Color_Red, 4dip)
        cvs.Invalidate
    End If
End Sub

If you want to draw multiple lines then use two layers (panel * 2 + cvs * 2) and draw the line on the first layer when the user lifts the finger.
 
Upvote 0

samannnn

Member
Full example:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Panel1 As B4XView
    Private cvs As B4XCanvas
    Private px, py As Int
End Sub

Public Sub Initialize
   
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    cvs.Initialize(Panel1)
End Sub


Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action = Panel1.TOUCH_ACTION_DOWN Then
        px = X
        py = Y
    Else
        cvs.ClearRect(cvs.TargetRect)
        cvs.DrawLine(px, py, X, Y, xui.Color_Red, 4dip)
        cvs.Invalidate
    End If
End Sub

If you want to draw multiple lines then use two layers (panel * 2 + cvs * 2) and draw the line on the first layer when the user lifts the finger.
thank you for your replay. I m not expert enough. could you attach an example?
Thank you in advance for your efforts.
 
Upvote 0

samannnn

Member
Time to learn how to use the designer.

thank you i can do it. but 2 questions:
1- how can i add line in my panel because when i LoadLayout("MainPage") ,it comes in front of my panel.
2.when i what to add some parameter to Initialize in log i get these errors " '(' expected " & "Undeclared variable 'avtivity' is used before it was assigned any value.". i attach my code.

B4X:
Dim pm As B4XPagesManager
pm.Initialize(MainG2)

B4X:
public Sub Initialize(p As Panel)

End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
   
    Root.LoadLayout("MainPage")
    cvs.Initialize(Panel1)
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.


Sub Panel1_Touch (Action As Int, X As Float, Y As Float)
    If Action = Panel1.TOUCH_ACTION_DOWN Then
'        px = X
'        py = Y
        px=WdsG2.leftcenter(0)
        py=WdsG2.topcenter(0)
    else If Action=Panel1.TOUCH_ACTION_UP Then
'        cvs.DrawLine(px, py, X, Y, xui.Color_Transparent, 10dip)
'        cvs.DrawCircle(px, py,30dip, xui.Color_Red,False, 4dip)
        cvs.ClearRect(cvs.TargetRect)
        cvs.Invalidate
        cvs.Release
        Root.RemoveAllViews
        For i=0 To 1
            WdsG2.SaveAnswer(i)=""
            WdsG2.savePnumber(i)=-1
            WdsG2.answerCount=0
        Next
    else if Action=Panel1.TOUCH_ACTION_MOVE Then
        saveX=x
        cvs.ClearRect(cvs.TargetRect)
        cvs.DrawLine(px, py, X, Y, xui.Color_Red, 4dip)
        cvs.Invalidate
        checkline
    Else

    End If
End Sub
 
Last edited:
Upvote 0
Top