AsyncLayoutInflater

  • 简介
  • 构造方法
  • inflate方法
  • inflateThread
  • 添加 request 初始化布局的请求

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

可以看到做了三件事情:

  • 创建 BasicInflater;
  • 创建 Handler;
  • 获取 InflateThread 对象;
  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);
            }
        }
    }
  • enqueue 函数;
    • 只是插入元素到 mQueue 队列中,如果元素过多那么是有排队机制的;
  • runInner 函数;
    • 运行于循环中,从 mQueue 队列取出元素;
    • 调用 inflate 方法;
    • 返回主线程;

此处提一个问题: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 一样,通过接口把事情抛到外部的调用方。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容