Constraints with once

A constraint is dynamically evaluated and this imposes some penalty on performance. Sometimes, you don’t want or don’t need a constraint to be evaluated continuously, but only when your object is initialized. For this, you can use the once keyword. For example, the constraint ${parent.width} in the view in Listing 6.4 is probably only required to be initialized once, because the canvas width never changes throughout its lifetime.

Listing 6.4. A constraint that gets evaluated all the time
<canvas>
    <view y="0" bgcolor="0xbbccdd" height="4"
            width="${parent.width}">
</canvas>

In this case, you can use once for better performance, as shown in Listing 6.5:

Listing 6.5. A constraint that gets evaluated once
<canvas>
    <view y="0" bgcolor="0xbbccdd" height="4"
            width="$once{parent.width}">
</canvas>

If no keyword is present between $ and {, the value of always is implied. Using once can help your application perform better.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset