Touch buffer problems

Skywalker

Member
Licensed User
Longtime User
In the German forum, we discuss currently the following problem.
You can download the example code (CanvasTest.zip) in this post:
http://www.b4x.com/forum/german-forum/14629-activity_touch-puffer-loeschen-2.html#post83193

Using a Canvas are randomly drawn some circles. Through gestures to the left or right, the graphic is redrawn.
But while the graph is drawn, none of the gestures should be processed.

Now the following happens:
The first time the chart represents the gestures are not processed. That is right.
Wipe it now several times to the left or right, then the graph is also drawn multiple times. It is processed thus a buffer, although this is not desired.
If you press "reset" the graph is redrawn, and when you wipe now over the screen, the gestures are not processed.
Why is this so?

What must be done so that while the graphics output, none of the gestures are recorded?

Sky
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem is here:
B4X:
For i = 0 To 50
 Canvas1.DrawCircle(Rnd(0,320),Rnd(0,220),10, Colors.RGB(0,0,180),False,1)
 Panel1.Invalidate 
 DoEvents
Next
Why do you need to call DoEvents (51 times)?
You should instead change the code to:
B4X:
For i = 0 To 50
 Canvas1.DrawCircle(Rnd(0,320),Rnd(0,220),10, Colors.RGB(0,0,180),False,1)
Next
Panel1.Invalidate
No DoEvents at all.
 
Upvote 0

Skywalker

Member
Licensed User
Longtime User
Hi Erel,

thank you. The problem is, I wish to see the drawing of the circles.
Without "DoEvents" I can only see the finished grafic.

"CanvasTest" is just a demo, my real code calculates fractals.
So i wish to see the drawings.

I think the problem is, that I can not call the Sub "Draw_the_circles" from "Panel1_touch". Everytime I do this, the gestures are buffered.

But i found a solution. It works fine, but i don't know if this is the best method.
I change the code, use a timer and a new boolean var.
The Touch routine check for left/right gestures, and if so, the variable "draw" is set to true.
Now the timer check every 500ms if the boolean "draw" is true and start the grafic output.

Why I can not call the drawing sub out of the Touch sub, so there is no buffering gestures?

Thank you for Basic4Android, Erel. I love it!
Sky

B4X:
'Activity module
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 Canvas1 As Canvas
Dim Label2 As Label
Dim Panel1 As Panel
Dim button1 As Button
Dim Rect1 As Rect
Dim active,draw As Boolean
Dim x_pos_old,y_pos_old,i As Int
Dim timer1 As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.loadlayout("layout")
Canvas1.Initialize(Panel1)
Rect1.Initialize(0dip, 0dip, 320dip, 220dip)
draw=False
timer1.Initialize("timer1", 500)
timer1.Enabled = True
End Sub

Sub Activity_Resume
Draw_the_circles
End Sub

Sub Draw_the_circles
timer1.Enabled = False
active=True
DoEvents
Label2.Text =""
Canvas1.DrawRect(Rect1, Colors.rgb(128,128,128), True, 1dip)
button1.Enabled=False

For i = 0 To 50 '50 good for Emulator. Increase for real device
 Canvas1.DrawCircle(Rnd(0,320),Rnd(0,220),10, Colors.RGB(0,0,180),False,1)
 Panel1.Invalidate 
 DoEvents
Next

button1.Enabled=True
Label2.Text="Ready"
active = False
timer1.Enabled = True
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub button1_click
clear_screen
Draw_the_circles
End Sub
Sub timer1_tick
If draw=True Then
 draw=False
 clear_screen
 Draw_the_circles
End If
End Sub

Sub Panel1_Touch (Action As Int, x As Float, y As Float) As Boolean 
If active = False Then ' Do this only if no circles are drawing
 Select Action
  Case Activity.ACTION_DOWN
   x_pos_old=x
   y_pos_old=y
  Case Activity.ACTION_UP
   If x_pos_old-x>150 Then ' Gesture to left
    draw=True
   End If
   If x-x_pos_old>150 Then ' Gesture to right
   draw=True
   End If
 End Select
Else
End If
Return True
End Sub
Sub clear_screen
Canvas1.DrawRect(Rect1, Colors.rgb(128,128,128), True, 1dip)
Panel1.Invalidate
End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…