Android ConstraintLayout性能改进-缺少操作 [英] Android ConstraintLayout performance improvements - Missing in action

查看:356
本文介绍了Android ConstraintLayout性能改进-缺少操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个自定义视图并插入了日志记录,以进行估计的性能比较

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);
    }

}

然后使用开始& "MyKeyboard"日志的结束时间...我最终得到以下值:

then using start & end times of "MyKeyboard" logs... I end up with the following values:

  • 带有指南14.32的ConstraintLayout
  • 具有链13.62的ConstraintLayout
  • LinearLayout(嵌套权重)4.88

这基于以下要点中的这些xml布局文件: - https://gist.github.com/CrandellWS/fc7946ea653cf90828580b3c00d8da57

That was based on these xml layouts files in the following gist: - https://gist.github.com/CrandellWS/fc7946ea653cf90828580b3c00d8da57

那么,如何使ConstraintLayout渲染的速度与嵌套的LinearLayout一样快?我可以做些什么或改变以使ConstraintLayout更接近LinearLayout性能?

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?

实际"键盘布局文件是已知的

请注意,无法在我的物理设备上使用Systrace https://stackoverflow.com/a/52836747/1815624 ...因此是最基本的性能测试方法...

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...

推荐答案

为了回答这个问题,我们必须绕道而行.

In order to answer this question, we'll have to take a detour for that.

我假设您已经阅读过有关ConstraintLayout内部工作方式的信息,所以让我坚持一下.是的,我同意ConstraintLayoutLinearLayout慢,但这只是在子视图的数量较少时.

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.

当您开始构建较大的布局(例如包含20-30个视图)时,ConstraintLayout会派上用场.如果您随后将使用LinearLayout或其他任何布局,例如说RelativeLayout,则最终将使用多个子级ViewGroups,并且布局图可能最终会像这样

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?

然后列表继续.

现在,使用这种布局,传统的ViewGroups最终将计算出比ConstraintLayout

Now, with such kind of Layout, traditional ViewGroups will end up computing more number of views than ConstraintLayout

因此,我们可以得出一个结论:没有ViewGroup是完美的!我们只需要根据需要使用它们即可.

So, we can come up with a conclusion that, no ViewGroup is perfect!! We just have to use them in accordance to our need..

奖金!RecyclerView内部应避免使用ConstraintLayout,因为它比其他布局多次调用onMeasure().

Bonus!! ConstraintLayout should be avoided inside RecyclerView because it calls onMeasure() multiple times than any other layout.

我曾经在ConstraintLayout上进行过研究,然后再将其应用于我的项目.

I once made some research on ConstraintLayout back then before applying it to my project.

这篇关于Android ConstraintLayout性能改进-缺少操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆