Android Question Using drawline for trail effect

tufanv

Expert
Licensed User
Longtime User
Hello,

I have a label with text "X" . X moves 10dip from top to bottom every 2 seconds. ( Like its moving constantly on radar ) . I need to draw dots for the last 3 movements. I mean for its position 6 seconds ago ( -3 movement ) i need a dot and for its position 4 seconds ago i need a dot and for its position 2 seconds ago i need another dot . So upside down it will look like this:

.
.
.
X

Later when i draw the new dot on 8th second i want to delete the first drawen dot so there will always be 3 dots on its behind. I tried to use drawline from canvas yesterday but i had problems deleting the 4th dot as it does not have removeline or something like that.

Can you point me to a right direction about it so i can focus on the right thing ? Thanks
 

strat

Active Member
Licensed User
Longtime User
I didn't write code now but you can use such method to plot it.

At every X moving, assign numbers to an array and draw dots and space into array coordinates

B4X:
dots_y[0]=Text_x.y-10dip
dots_y[1]=Text_x.y-20dip
dots_y[2]=Text_x.y-30dip
dots_y[3]=Text_x.y-40dip

Plot(Text_x.x,dots_y[0])
Plot(Text_x.x,dots_y[1])
Plot(Text_x.x,dots_y[2])
Space(Text_x.x,dots_y[3])



Sub plot(x,y as int)
...
..
Sub Space(x,y as int)
..
..

Text_x.x and Text_x.y Your Text's coordinates
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thanks Strat !
I will try it now.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User

Problem here is the X does not always travel 10 dip to bottom on each move. If the degree it is moving is 180 ( which means vertically down it moves 10 dip= but if it moves at 140 degress for example it is less than 10 dips ( according to cos calculation ). Also in the sub plot and space do i just need to use canvas.drawline ?
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
Try this. Touch the screen and move. After movinx text X, puts after 3 big dots and a space.

B4X:
##Region  Project Attributes
    #ApplicationLabel: draw
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

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
    Private pnl As Panel
    Private lbl As Label
    Private old_x,old_y As Int
    Private first As Boolean
    Private coordx(5) As Int
    Private coordy(5) As Int
    Private c As Canvas
    Private uzunluk As Float
    Private xf,yf As Float
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    pnl.Initialize("pnl")
    lbl.Initialize("lbl")
    Activity.AddView(lbl,0,0,100%x,10%y)
    Activity.AddView(pnl,0,0,100%x,100%y)
    first=True
    c.Initialize(pnl)
    'pnl.Color=Colors.White
    lbl.Text="X"
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub pnl_Touch (Action As Int, X As Float, Y As Float)
    xf=X-old_x
    yf=Y-old_y
    uzunluk=Sqrt(xf*xf+yf*yf)
    If uzunluk>=50 Then
        lbl.Left=X
        lbl.Top=Y
        add_to_array(X,Y)
        draw_dots
        old_x=X
        old_y=Y
    End If
End Sub

Sub draw_dots
    'Dim c As Canvas
    c.Initialize(pnl)
    For i=1 To 3
        c.DrawCircle(coordx(i),coordy(i),5dip,Colors.White,True,1dip)
    Next
    c.DrawCircle(coordx(4),coordy(4),5dip,Colors.Black,True,1dip)
End Sub

Sub add_to_array(a_x As Int ,a_y As Int)
    If first Then
        For i=0 To 4
            coordx(i)=a_x
            coordy(i)=a_y
        Next
        first=False
    Else
        coordx(4)=coordx(3)
        coordx(3)=coordx(2)
        coordx(2)=coordx(1)
        coordx(1)=coordx(0)
        coordx(0)=a_x
        coordy(4)=coordy(3)
        coordy(3)=coordy(2)
        coordy(2)=coordy(1)
        coordy(1)=coordy(0)
        coordy(0)=a_y
    End If
End Sub
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User


Thank you for your time ! it works perfect. Just a small note: When white dots disappear they stay as black dots on the screen( all the previous trails ) is it normal or is there way to completely remove them ?
 
Upvote 0

strat

Active Member
Licensed User
Longtime User
Thank you for your time ! it works perfect. Just a small note: When white dots disappear they stay as black dots on the screen( all the previous trails ) is it normal or is there way to completely remove them ?

I assume screen color is black. You can select the background color for last dot in order to complete disappear dots.
If panel has a picture, instead of putting last dot, original picture part should be paste at last coordinates. To do it, first, the part of picture should be copied to memory.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Thanks very much ! I will set the backcolor black so it wont be a problem !.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…