Android Question how to avoid using the doevents

LordZenzo

Well-Known Member
Licensed User
Longtime User
if I have to use more commands like that
mMano.SetLayoutAnimated (MDURATION, x, y, mMano.Width, mMano.Height)
to create a complex animation you only see the last command
if I insert a sleep (0) the animations are reduced to some flashes in the beginning positions, except the last one
 

Star-Dust

Expert
Licensed User
Longtime User
B4X:
mMano.SetLayoutAnimated (MDURATION_Animation_1, x, y, mMano.Width, mMano.Height)
Sleep(MDURATUON_Animation_1)
mMano.SetLayoutAnimated (MDURATION_Animation_2, x, y, mMano.Width, mMano.Height)
Sleep(MDURATUON_Animation_2)
mMano.SetLayoutAnimated (MDURATION_Final, x, y, mMano.Width, mMano.Height)
Sleep(MDURATUON_Final)

Please note that sometimes the SetLayoutAnimation animations do not seem to start right away, and the Sleep of the same duration does not seem to be enough to make the animation visible.
I would also add a 100 or 200 milliseconds more
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
unfortunately it does not work because the code is not exactly like that,
the animation is set inside a sub called by another sub
and sleep causes the diver to exit immediately
the example closest to my code is this
B4X:
public sub test
      Animation (2000,100,100)
      Animation (2000,300,200)
      ......
end sub
private sub Animation (duration as long, X as int, Y as int)
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
end sub
due to the complexity of the calling code, inserting sleep is not "simple" because the Animation sub is inserted in what will be a library
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
MT1 = DateTime.Now
Do Until (DateTime.Now-mT1)> k (1)
mMano.Invalidate

Loop
Sorry to say but it is a very bad code and should never be used.

You need to learn how to use resumable subs that return value. Watch the resumable subs video tutorial: https://www.b4x.com/etp.html

Simple solution:
B4X:
public sub test
      Animation (2000,100,100)
      Sleep(2000)
      Animation (2000,300,200)
      Sleep(200)
      ......
end sub
private sub Animation (duration as long, X as int, Y as int)
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
end sub

If you want to sleep inside the Animation sub:
B4X:
public sub test
      Wait For (Animation (2000,100,100)) Complete (Unused As Boolean)
      Wait For (Animation (2000,200,300)) Complete (Unused As Boolean)

      ......
end sub
private sub Animation (duration as long, X as int, Y as int) As ResumableSub
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
      Sleep(Duration)
      Return True
end sub
 
Last edited:
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Note: @Erel's 2nd example left out the sleep inside the Animation sub (mentions it, just left it out)
B4X:
public sub test
      Wait For (Animation (2000,100,100)) Complete (Unused As Boolean)
      Wait For (Animation (2000,200,300)) Complete (Unused As Boolean)

      ......
end sub
private sub Animation (duration as long, X as int, Y as int) As ResumableSub
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
      Sleep(200)
      Return True
end sub
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
Sorry to say but it is a very bad code and should never be used.

You need to learn how to use resumable subs that return value. Watch the resumable subs video tutorial: https://www.b4x.com/etp.html

Simple solution:
B4X:
public sub test
      Animation (2000,100,100)
      Sleep(2000)
      Animation (2000,300,200)
      Sleep(200)
      ......
end sub
private sub Animation (duration as long, X as int, Y as int)
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
end sub

If you want to sleep inside the Animation sub:
B4X:
public sub test
      Wait For (Animation (2000,100,100)) Complete (Unused As Boolean)
      Wait For (Animation (2000,200,300)) Complete (Unused As Boolean)

      ......
end sub
private sub Animation (duration as long, X as int, Y as int) As ResumableSub
      mMano.setLayountAnimatio (duration, X, Y, mMano.width, mMano.heigth)
      Return True
end sub
it does not work because the setLayountAnimated is asynchronous
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
Looking at post #3, looks like duration should also be used for sleep?

And?

and then move on to the next commands before the end of the animation creating a problem of continuity of the animation itself
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
the problem is always the same
sleep returns control to the flow of commands
I need only the "drawing" to be updated in the positions of the animation without the flow passing to the control that sets the successive animations
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
the problem is always the same
sleep returns control to the flow of commands
That's why you use Wait For. You may need to use multiple levels of Wait For, but that's what you need to use.

If test is called from another location in your app, you need to make test resumable also and wait for it where it is called.
 
Upvote 0

LordZenzo

Well-Known Member
Licensed User
Longtime User
That's why you use Wait For. You may need to use multiple levels of Wait For, but that's what you need to use.

If test is called from another location in your app, you need to make test resumable also and wait for it where it is called.
no, the position test with the LayountAnimated does not work
 
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
DoEvents' deprecation
i don't think it belongs doevents
sleep(0) = doevents

i think your need is a animation list which not exists as feature.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
the position test with the LayountAnimated does not work
How about showing all the code. From the code that calls test. From the code that calls the code that calls test. etc. Test and the animations.
 
Upvote 0
Top