Android Question Jerky Animation and Timer Interaction

RJB

Active Member
Licensed User
Longtime User
Erel,
New thread and example code as suggested:

I'm having a problem with jerky animation. I've tried setting the hardware acceleration in the manifest but it doesn't seem to have any effect. Should this now be set in the project or activity attributes and if so how? I'm moving photographic images.
The animation also seems to have an adverse effect on timers where the time period can be, for example, stretched from 20mS to 80/ 90mS during the transition. Is that normal?

Code example:
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim timer1 As Timer
    Dim timer2 As Timer
    Dim timer3 As Timer
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 image As Bitmap
    Dim displayslide1 As ImageView
    Dim displayslide2 As ImageView
    Dim slidepanel As Panel
    Dim animate1 As Animation
    Dim animate2 As Animation
    Dim thistime As Long
    Dim lasttime As Long
    Dim interval1 As Int = 20
    Dim interval2 As Int = 1000
    Dim interval3 As Int = 250
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")
    timer1.Initialize("timer1",interval1)
    timer2.Initialize("timer2",interval2)
    timer3.Initialize("timer3",interval3)
    timer1.Enabled = True
    timer3.Enabled = True
    slidepanel.Initialize("")
    Activity.AddView(slidepanel,10dip,10dip,800dip,500dip)
    displayslide1.Initialize("")
    displayslide2.Initialize("")
    displayslide1.Gravity = Gravity.FILL
    displayslide2.Gravity = Gravity.FILL
    slidepanel.AddView(displayslide1, 0dip, 0dip, slidepanel.width, slidepanel.height)
    slidepanel.AddView(displayslide2,0dip, 0dip, slidepanel.width, slidepanel.height)
    image.InitializeSample(File.DirRootExternal & "/Pictures/", "image1.jpg",slidepanel.width, slidepanel.height)
    displayslide1.Bitmap = image
    image.InitializeSample(File.DirRootExternal & "/Pictures/", "image2.jpg",slidepanel.width, slidepanel.height)
    displayslide2.Bitmap = image
    animate1.InitializeTranslate("animations",0,0,-slidepanel.Width,0)
    animate1.duration = interval2
    animate2.InitializeTranslate("",slidepanel.Width,0,0,0)
    animate2.duration = interval2
    animate1.Start(displayslide1)
    animate2.start(displayslide2)
 
End Sub
 
Sub timer1_tick
    thistime = DateTime.now
    Log(thistime & " : " & (thistime - lasttime) & " : " &  (thistime - lasttime - interval1))
    lasttime = thistime
End Sub
 
Sub Animations_Animationend
    Log("stop")
    timer2.Enabled = True
End Sub
 
Sub timer2_tick
    timer2.Enabled = False
    Log("start")
    animate1.Start(displayslide1)
    animate2.start(displayslide2)
End Sub
 
Sub timer3_tick
    For i = 0 To 5000
'        DoEvents
    Next
End Sub

The loop within timer3_tick illustrates the effect of other code running and makes the animation jerky. Enabling the 'DoEvents' solves it.
Timer1_tick shows the effect of the animation running has on that timer with the period varying up to four times its interval setting when the transition is happening.
Any suggestions please?
 

thedesolatesoul

Expert
Licensed User
Longtime User
Firstly,
are you saying that the animations slow down the timer?
or does the timer slow down the animations?

Remember that everything is happening on the same thread, so any long operations will block the UI for a while.
However, I dont expect the animation to slow down the timer, but the other way round.
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
Firstly,
are you saying that the animations slow down the timer?
or does the timer slow down the animations?

Remember that everything is happening on the same thread, so any long operations will block the UI for a while.
However, I dont expect the animation to slow down the timer, but the other way round.
Hi,
The animation slows down the timer. The timer may have an effect on the animation but it's not noticeable.
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
What is the purpose of timer3? It will only cause damage...
Yes, it's only demo code to show the problem. timer3 is only there to simulate other code running (and cause the damage) and help in diagnostics.
 
Upvote 0

RJB

Active Member
Licensed User
Longtime User
It is hard to help without a working example... (which doesn't include a busy loop like the one above)
Sorry, the idea was to make the code as simple as possible!
The timer3 loop just represents the effect of other code running and interfering with the timer/ animation. The real code does the same thing, i.e. makes the animation jerky and the timers even mode erratic than normal.
Taking timer3 out altogether leaves the animation smoother but still shows the effect of the animation on timer1 (in the log between the 'start' and 'stop').
Reading other posts it looks like I may have to resort to 'timing loops' for both animation and accurate timers. I really don't want to do that but if there's no other way!
I'd appreciate any other suggestions though.
Thanks
 
Upvote 0
Top