Android LayoutInflater.inflate耗时解决方法

最近项目中收到公司APM反馈的一个ANR问题,具体堆栈指向LayoutInflater.inflate。

这句原生调用怎么会发生ANR呢?

于是使用Android Studio的 Layout Inspector工具查看了下View层次,发现View层次过多,并且有复杂的自定义View,这个应该就是耗时的主因。

针对复杂的布局,我们通常的布局优化手段:简化布局的层级;include和merge结合使用;ViewStub 按需加载;减少使用wrap_content,最好使用固定宽高或者match_parent,减少计算;ConstraintLayout使用等。

另外还有其他的优化手段:如AsyncLayoutInflater异步加载布局。笔者使用这种方式解决了LayoutInflater.inflate的ANR问题。此外我们还可以在super.onCreate之前使用LayoutInflaterCompat.setFactory2来监控控件加载耗时。

异步加载布局使用如下:inflate 成功后 再调用setContentView和UI控件初始化。

 new AsyncLayoutInflater(MainActivity.this).inflate(R.layout.activity_main, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
            @Override
            public void onInflateFinished(@NonNull View view, int resid, @Nullable ViewGroup parent) {
                setContentView(view);
                initUI();
            }
        });

 监控控件加载耗时:

        LayoutInflaterCompat.setFactory2(getLayoutInflater(), new LayoutInflater.Factory2() {
            @Nullable
            @Override
            public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
                long startTime = System.currentTimeMillis();
                View view = getDelegate().createView(parent, name, context, attrs);
                long cost = System.currentTimeMillis() - startTime;
                Log.d("LayoutInflateTest", "加载控件:" + name + "耗时:" + cost);
                return view;
            }

            @Nullable
            @Override
            public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
                return null;
            }
        });

猜你喜欢

转载自blog.csdn.net/xiaobaaidaba123/article/details/123971441
今日推荐