AsyncLayoutInflater

2020-03-30  本文已影响0人  ColorfulXL

1、AsyncLayoutInflater 简介

AsyncLayoutInflater 是来帮助做异步加载 layout 的,inflate(int, ViewGroup, OnInflateFinishedListener) 方法运行结束之后 OnInflateFinishedListener 会在主线程回调返回 View;这样做旨在 UI 的懒加载或者对用户操作的高响应。

简单的说我们知道默认情况下 setContentView 函数是在 UI 线程执行的,其中有一系列的耗时动作:Xml的解析、View的反射创建等过程同样是在UI线程执行的,AsyncLayoutInflater 就是来帮我们把这些过程以异步的方式执行,保持UI线程的高响应。

AsyncLayoutInflater 比较简单,只有一个构造函数及普通调用函数:inflate(int resid, ViewGroup parent, AsyncLayoutInflater.OnInflateFinishedListener callback),使用也非常方便。

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new AsyncLayoutInflater(AsyncLayoutActivity.this)
                .inflate(R.layout.async_layout, null, new AsyncLayoutInflater.OnInflateFinishedListener() {
                    @Override
                    public void onInflateFinished(View view, int resid, ViewGroup parent) {
                        setContentView(view);
                    }
                });
        // 别的操作
    }

2、AsyncLayoutInflater 构造函数

AsyncLayoutInflater 的源码非常简单,总共只有170行代码,我们就从调用的入口来看下。

    public AsyncLayoutInflater(@NonNull Context context) {
        mInflater = new BasicInflater(context);
        mHandler = new Handler(mHandlerCallback);
        mInflateThread = InflateThread.getInstance();
    }

可以看到做了三件事情:

  1. BasicInflater 继承自 LayoutInflater,只是覆写了 onCreateView:优先加载这三个前缀的 Layout,然后才按照默认的流程去加载,因为大多数情况下我们 Layout 中使用的View都在这三个 package 下
    private static class BasicInflater extends LayoutInflater {
        private static final String[] sClassPrefixList = {
            "android.widget.",
            "android.webkit.",
            "android.app."
        };
        @Override
        protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
            for (String prefix : sClassPrefixList) {
                try {
                    View view = createView(name, prefix, attrs);
                    if (view != null) {
                        return view;
                    }
                } catch (ClassNotFoundException e) {
                }
            }
            return super.onCreateView(name, attrs);
        }
    }
  1. 创建 Handler 和它普通的作用一样,就是为了线程切换,AsyncLayoutInflater 是在异步里 inflate layout,那创建出来的 View 对象需要回调给主线程,就是通过 Handler 来实现的
  2. InflateThread 从名字上就好理解,是来做 Inflate 工作的工作线程,通过 InflateThread.getInstance 可以猜测 InflateThread 里面是一个单例,默认只在一个线程中做所有的加载工作,这个类我们会在下面重点分析。

3、inflate

    @UiThread
    public void inflate(@LayoutRes int resid, @Nullable ViewGroup parent,
            @NonNull OnInflateFinishedListener callback) {
        if (callback == null) {
            throw new NullPointerException("callback argument may not be null!");
        }
        InflateRequest request = mInflateThread.obtainRequest();
        request.inflater = this;
        request.resid = resid;
        request.parent = parent;
        request.callback = callback;
        mInflateThread.enqueue(request);
    }

首先会通过 InflateThread 去获取一个 InflateRequest,其中有一堆的成员变量。为什么需要这个类呢?因为后续异步 inflate 需要一堆的参数(对应 InflateRequest 中的变量),会导致方法签名过长,而使用 InflateRequest 就避免了很多个参数的传递。

    private static class InflateRequest {
        AsyncLayoutInflater inflater;
        ViewGroup parent;
        int resid;
        View view;
        OnInflateFinishedListener callback;

        InflateRequest() {
        }
    }

接下来对 InflateRequest 变量赋值之后会将其加到 InflateThread 中的一个队列中等待执行

    public void enqueue(InflateRequest request) {
        try {
            mQueue.put(request);
        } catch (InterruptedException e) {
            throw new RuntimeException(
                    "Failed to enqueue async inflate request", e);
        }
    }

4、InflateThread

    private static class InflateThread extends Thread {
        private static final InflateThread sInstance;
        static {
            // 静态代码块,确保只会创建一次,并且创建即start。
            sInstance = new InflateThread();
            sInstance.start();
        }

        public static InflateThread getInstance() {
            return sInstance;
        }

        private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10);// 异步inflate的缓存队列;
        private SynchronizedPool<InflateRequest> mRequestPool = new SynchronizedPool<>(10);// Todo

        // Extracted to its own method to ensure locals have a constrained liveness
        // scope by the GC. This is needed to avoid keeping previous request references
        // alive for an indeterminate amount of time, see b/33158143 for details
        public void runInner() {
            InflateRequest request;
            try {
                request = mQueue.take();// 从队列中取一个request。
            } catch (InterruptedException ex) {
                // Odd, just continue
                Log.w(TAG, ex);
                return;
            }

            try {
                request.view = request.inflater.mInflater.inflate(
                        request.resid, request.parent, false);// Inflate layout
            } catch (RuntimeException ex) {
                // Probably a Looper failure, retry on the UI thread
                Log.w(TAG, "Failed to inflate resource in the background! Retrying on the UI"
                        + " thread", ex);
            }
            Message.obtain(request.inflater.mHandler, 0, request)
                    .sendToTarget();// 返回主线程执行
        }

        @Override
        public void run() {
            while (true) {
                runInner();// 循环,但其实不会一直执行
            }
        }

        public InflateRequest obtainRequest() {
            InflateRequest obj = mRequestPool.acquire();
            if (obj == null) {
                obj = new InflateRequest();
            }
            return obj;
        }

        public void releaseRequest(InflateRequest obj) {
            obj.callback = null;
            obj.inflater = null;
            obj.parent = null;
            obj.resid = 0;
            obj.view = null;
            mRequestPool.release(obj);
        }

        public void enqueue(InflateRequest request) {
            try {
                mQueue.put(request);// 添加到缓存队列中
            } catch (InterruptedException e) {
                throw new RuntimeException(
                        "Failed to enqueue async inflate request", e);
            }
        }
    }

此处提一个问题:runInner 运行于循环中,会一直在执行吗?

实际上不是的,mQueue 队列的类型是 ArrayBlockingQueue ,这是一个“生产者-消费者”的模型,如果队列中没有元素,那么 mQueue.take() 就会处于等待状态,直到 mQueue.put 唤醒才会继续执行。


5.添加 request 初始化布局的请求: AsyncLayoutInflater.inflate(xxx)

当调用该方法时,会有一系列步骤:

  1. 创建 InflateRequest 请求;
  2. 通过 mInflateThread.enqueue(request) 把该请求放入到 mInflateThread 子线程里面

源码如下:

@UiThread
public void inflate(@LayoutRes int resid, @Nullable ViewGroup parent,
            @NonNull OnInflateFinishedListener callback) {
    if (callback == null) {
        throw new NullPointerException("callback argument may not be null!");
    }
    InflateRequest request = mInflateThread.obtainRequest();
    request.inflater = this;
    request.resid = resid;
    request.parent = parent;
    request.callback = callback;
    mInflateThread.enqueue(request);
}

从代码中可以看到,每一次调用 inflate(xxx) 方法都会新创建一个 InflateRequest, 并且把该 request 加入 mInflateThread 的队列中。

2.1 创建 InflateRequest 请求

而在 InflateThread 中有一个队列 mQueue 用来存放 InflateRequest 请求

private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10);

InflateThread 线程在 start() 之后,会去调用 runInner()尝试获取 inflateRequest 然后执行对应逻辑。

@Override
public void run() {
    while (true) {
        runInner();
    }
}

注:这里并不会是一个死循环, 因为 mQueue.take() 方法。
ArrayBlockingQueue 是会产生阻塞的队列,在 take() 方法中,如果 count == 0, 则会一直陷入 notEmpty.await()

ArrayBlockingQueuetake() 方法源码:

public E take() throws InterruptedException {
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
        // 这里会一直等待,直到有消息为止
        while (count == 0)
            notEmpty.await();
        return dequeue();
    } finally {
        lock.unlock();
    }
}

2.2 通过 mInflateThread.enqueue(request) 添加请求后,mQueue 不为空

当该 mQueue 里面可以获取到 request. 则会通过 inflater.inflate(xxx) 在子线程中完成 view 的构建,并通过 Message 发送消息给对应的 handler 处理。代码如下:

public void runInner() {
     InflateRequest request;
     try {
         // 获取是否有请求
         request = mQueue.take();
     } catch (InterruptedException ex) {
         // Odd, just continue
         Log.w(TAG, ex);
         return;
     }
    // 
     try {
         request.view = request.inflater.mInflater.inflate(
                        request.resid, request.parent, false);
     } catch (RuntimeException ex) {
         // Probably a Looper failure, retry on the UI thread
         Log.w(TAG, "Failed to inflate resource in the background! Retrying on the UI" + " thread", ex);
    }
    Message.obtain(request.inflater.mHandler, 0, request).sendToTarget();
}

3. Handler 里面处理 Message 发送的消息, 把结果返回给 UI 线程

通过 Message 发送消息后,真正接受消息的地方是在 HandlerhandleMessage(xxx) 方法里面,
此时已经切回到了 UI 线程中:

@Override
public boolean handleMessage(Message msg) {
    InflateRequest request = (InflateRequest) msg.obj;
    if (request.view == null) {
        request.view = mInflater.inflate(request.resid, request.parent, false);
    }
    request.callback.onInflateFinished(request.view, request.resid, request.parent);
    mInflateThread.releaseRequest(request);
    return true;
}

代码逻辑:

  1. msg.obj 中获取到 InflateRequest
  2. 判断 request.view 是否为 null
  3. 如果为空,则重新 inflate ,此时是在 UI 线程中进行的,和一般的初始化一样;
  4. 通过接口 OnInflateFinishedListener 通知外部,并把得到的 view 传递出去

注:在这里做了兼容,防止在异步中 inflate 失败,做了判断

对外暴露的接口:AsyncLayoutInflater.OnInflateFinishedListener,类似与最简单的 View.OnClickListener 一样,通过接口把事情抛到外部的调用方。

上一篇 下一篇

猜你喜欢

热点阅读