Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many times does ConstraintLayout measure each of its children?

RelativeLayout measures all its children twice. This can cause performance issues. Does constraint layout only measure its children once each?

like image 928
Justin Watkins Avatar asked Jun 09 '16 15:06

Justin Watkins


People also ask

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.

What is the use of ConstraintLayout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

Why is ConstraintLayout faster?

This is because ConstraintLayout allows you to build complex layouts without having to nest View and ViewGroup elements. When running the Systrace tool for the version of our layout that uses ConstraintLayout , you see far fewer expensive measure/layout passes during the same 20-second interval.

What is the difference between ConstraintLayout and LinearLayout?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.


1 Answers

ConstraintLayout requires up to two measure passes.

If you look at the ConstraintLayout's source, you'll see that its onMeasure() method first measures its children inside an internalMeasureChildren() utility method. Next, it evaluates some constraints. Finally, ConstraintLayout calls child.measure() on its children a second time inside a loop.

Source: decompiled the class files, since the source isn't available at this time.

like image 57
Brian Attwell Avatar answered Sep 22 '22 12:09

Brian Attwell