Android Question How easiest to do a view "HitTest"

TheMightySwe

Active Member
Licensed User
Longtime User
Hi, If you have a ScrollView2D and hav a bunch of buttons that you can move, how do you easiest preform a "hit test" so you can prevent views to be placed on top of each other?
 

TheMightySwe

Active Member
Licensed User
Longtime User
I already do that, it works a bit "jumpy" when it's many buttons, thats I asked i anyone knew any simple way. If there was any built in function that I didn't know about.
 
Upvote 0

TheMightySwe

Active Member
Licensed User
Longtime User
Every movement, it's not a huge problem. on most devices it works just fine.

I was looking for something like the function isViewUnder(X,Y) for widgets.

https://developer.android.com/reference/android/support/v4/widget/ViewDragHelper.html

B4X:
Sub HitTest(SV2D As ScrollView2D, MovedButton As Button) As Boolean

    ' Check the movment against margins
    Dim ViewLeft As Int = MovedButton.Left
    Dim ViewTop As Int = MovedButton.Top
    Dim ViewWidth As Int = MovedButton.Width
    Dim ViewHeight As Int = MovedButton.Height

    If ViewLeft < InnerMargin Then Return True 
    If ViewTop < InnerMargin Then  Return True
    If ViewLeft + ViewWidth > SV2D.Width - InnerMargin Then  Return True
    If ViewTop + ViewHeight > SV2D.Height - InnerMargin Then  Return True

    For x = 0 To SV2D.Panel.NumberOfViews - 1
   
        Dim Left, Width, Height, Top As Int
       
        Dim TempView As Button
        TempView = SV2D.Panel.GetView(x)

        If TempView = MovedButton Then Continue

        ' Add margins
       
        Left = TempView.Left - InnerMargin
        Top = TempView.Top - InnerMargin
        Width = TempView.Width + (InnerMargin * 2) ' Add the left and right margin
        Height = TempView.Height + (InnerMargin * 2) ' Add the top and bottom margin

        ' Check for hit in LeftTop corner
        If ViewLeft > Left AND ViewLeft < (Left + Width) AND ViewTop > Top AND ViewTop < (Top + Height) Then Return True

        ' Check for hit in LeftBottom corner
        If ViewLeft > Left AND ViewLeft < (Left + Width) AND (ViewTop + ViewHeight) > Top AND (ViewTop + ViewHeight) < (Top + Height) Then Return True

        ' Check for hit in RightTop corner
        If (ViewLeft + ViewWidth)  > Left AND (ViewLeft + ViewWidth) < (Left + Width) AND ViewTop > Top AND ViewTop < (Top + Height) Then Return True

        ' Check for hit in RightBottom corner
        If (ViewLeft + ViewWidth) > Left AND (ViewLeft + ViewWidth) < (Left + Width) AND (ViewTop + ViewHeight) > Top AND (ViewTop + ViewHeight) < (Top + Height) Then Return True
   
    Next

    Return False
   
End Sub
 
Upvote 0

TheMightySwe

Active Member
Licensed User
Longtime User
Release was much better, the Logging was the problem I discovered before.

The HitTest functions very good.

Thanks for the help.
 
Upvote 0
Top