问题
I have created a custom view and inserted logging for an estimated performance comparison
public class CustomInAppKeyboard extends LinearLayoutCompat {
private static final String TAG = "MyKeyboard";
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(BuildConfig.DEBUG){
Log.e("CustomInAppKeyboard", "w:" + widthMeasureSpec + " :: " + MeasureSpec.toString(widthMeasureSpec));
Log.e("CustomInAppKeyboard", "h:" + heightMeasureSpec + " :: " + MeasureSpec.toString(heightMeasureSpec));
}
}
public CustomInAppKeyboard(Context context) {
this(context, null, 0);
}
public CustomInAppKeyboard(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomInAppKeyboard(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
LayoutInflater.from(context).inflate(R.layout.keyboard_alphanumeric, this, true);
}
}
then using start & end times of "MyKeyboard" logs... I end up with the following values:
- ConstraintLayout withguides 14.32
- ConstraintLayout with chains 13.62
- LinearLayout (nested weights) 4.88
That was based on these xml layouts files in the following gist: - https://gist.github.com/CrandellWS/fc7946ea653cf90828580b3c00d8da57
So how can I get the ConstraintLayout to render as fast as the nested LinearLayout? What could I do differently or change to get the ConstraintLayout to more closely match the LinearLayout performance?
"actual" Keyboard layout files are known to be different
Note that there is an inability to use Systrace on my physical device https://stackoverflow.com/a/52836747/1815624 ... hence the rudimentary performance test method...
回答1:
In order to answer this question, we'll have to take a detour for that.
I am assuming that you have read about how ConstraintLayout
works internally, so let me just stick to the point. Yes, I agree that ConstraintLayout
is slower than LinearLayout
but it's only when the number of child views are less in number.
When you start building larger layouts, say which consist of 20-30 Views, the ConstraintLayout
comes handy. If you'll then use LinearLayout
or any other layout, say RelativeLayout
then you'll end up using multiple child ViewGroups
and your Layout Graph might end up like this
LinearLayout(orientation vertical)
-> SomeChildView (let's say a TextView)
-> LinearLayout (orientation horizontal)
-> ChilView 1 -> ChildView 2
-> ImageView
-> ButtonView
-> ViewGroup (FrameLayout)
-> ImageView1
-> idk, maybe TextView?
and the list goes on.
Now, with such kind of Layout, traditional ViewGroups
will end up computing more number of views than ConstraintLayout
So, we can come up with a conclusion that, no ViewGroup is perfect!! We just have to use them in accordance to our need..
Bonus!! ConstraintLayout
should be avoided inside RecyclerView
because it calls onMeasure()
multiple times than any other layout.
I once made some research on ConstraintLayout
back then before applying it to my project.
来源:https://stackoverflow.com/questions/52847213/android-constraintlayout-performance-improvements-missing-in-action