Animate calls a function repeatedly like setInterval() but with the current time (Date.getTime()) and the difference to the last call in milliseconds as parameter.
To stop the animation loop you can pass null as first argument.
You can not have multiple animation loops at once.
When using cfg.NoDom; you cannot use JavaScripts setInterval but only the Animate function.
Note: The NoDom option is currently not available due to GooglePlays 64bit requirement since August 2019.
Comparison
normal:
setInterval: about 242 calls per second
app.Animate: about 217 calls per second
with “NoDom” option:
setInterval: error
app.Animate: up to 1000 calls per second
Example - Multiple Animation loops
function OnStart()
{
lay = app.CreateLayout("Linear");
txt1 = app.CreateText("0");
lay.AddChild(txt1);
txt2 = app.CreateText("0");
lay.AddChild(txt2);
app.AddLayout(lay);
app.Animate(loop, 10);
}
var t1 = 0, t2 = 0, n1 = 0, n2 = 0;
function loop(t, dt)
{
if(t - t1 >= 500)
{
txt1.SetText(++n1 + "\tdt: " + (t - t1));
t1 = t;
}
if(t - t2 >= 300)
{
txt2.SetText(++n2 + "\tdt: " + (t - t2));
t2 = t;
}
}
from native import app
def OnStart():
global txt1, txt2, n1, n2
lay = app.CreateLayout("Linear")
txt1 = app.CreateText("0")
lay.AddChild(txt1)
txt2 = app.CreateText("0")
lay.AddChild(txt2)
app.AddLayout(lay)
app.Animate(loop, 10)
t1 = 0
t2 = 0
n1 = 0
n2 = 0
def loop(t, dt):
if t - t1 >= 500:
txt1.SetText(str(++n1) + "\tdt: " + str(t - t1))
t1 = t
if t - t2 >= 300:
txt2.SetText(str(++n2) + "\tdt: " + str(t - t2))
t2 = t
Example - Digital Clock
function OnStart()
{
app.SetOrientation( "Portrait" );
app.SetDebugEnabled( false );
lay = app.CreateLayout( "Linear", "FillXY,VCenter" );
txt = app.CreateText( "", -1, -1, "multiline" );
txt.SetTextSize( 30 );
lay.AddChild( txt );
app.AddLayout( lay );
app.Animate( OnAnimate, 30 );
}
function OnAnimate( time, dtime )
{
txt.SetText( new Date().toLocaleString() + "\n" + time );
}
from native import app
def OnStart():
global txt
app.SetOrientation("Portrait")
app.SetDebugEnabled(False)
lay = app.CreateLayout("Linear", "FillXY,VCenter")
txt = app.CreateText("", -1, -1, "multiline")
txt.SetTextSize(30)
lay.AddChild(txt)
app.AddLayout(lay)
app.Animate( OnAnimate, 30 );
app.Animate(OnAnimate, 30)
def OnAnimate(time, dtime):
txt.SetText(str(new Date().toLocaleString()) + "\n" + str(time))
Example - SpeedTest
cfg.No_Dom;
var ltime = Date.now(), c = 0;
function OnStart()
{
lay = app.CreateLayout( "Linear", "FillXY,VCenter" );
txt = app.CreateText( "", .5, .1, "left" );
lay.AddChild( txt );
app.AddLayout( lay );
app.Animate(OnAnimate, 1000);
}
function OnAnimate( time, dtime )
{
c++;
if( time - ltime >= 1000 ) {
txt.SetText( c + " cps" );
ltime = time;
c = 0;
}
}
from native import app
import time as Date
ltime = Date.now()
c = 0
def OnStart():
global txt
lay = app.CreateLayout("Linear", "FillXY,VCenter")
txt = app.CreateText("", .5, .1, "left")
lay.AddChild(txt)
app.AddLayout(lay)
app.Animate(OnAnimate, 1000);
app.Animate(OnAnimate, 1000)
def OnAnimate(time, dtime):
c += 1
if time - ltime >= 1000:
txt.SetText(str(c) + " cps")
ltime = time
c = 0