布局异步加载 - AsyncLayoutInflater

前沿

我们知道布局加载的两大特性瓶颈,通过操作将XML加载到内存中并进行解析和反射创建View。

当xml文件过大或者页面层级过深,布局的加载就会较为耗时。

我们知道,当主线程进行一些耗时操作可能就会导致页面卡顿,更严重的可能会产生ANR,所以我们来进行布局加载优化。

解决这个问题有两种思路,直接解决和侧面缓解。

直接解决就是不使用IO和反射技术,我们这里介绍侧面缓解,即将布局加载和解析放在子线程中,等到inflate操作完成后再将结果回调到主线程中,即使用Android为我们提供的AsyncLayoutInflater类来进行异步布局加载。

AsyncLayoutInflater用法

AsyncLayoutInflater的使用非常简单,就是把setcontentView和一些View的初始化操作都放到了onInflateFinished回调中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new AsyncLayoutInflater(this).inflate(R.layout.activity_main,null, new AsyncLayoutInflater.OnInflateFinishedListener(){
        @Override
        public void onInflateFinished(View view, int resid, ViewGroup parent) {
            setContentView(view);
            rv = findViewById(R.id.tv);
            rv.setLayoutManager(new V7LinearLayoutManager(MainActivity.this));
            rv.setAdapter(new RightRvAdapter(MainActivity.this));
        }
    });

}

AsyncLayoutInflater源码分析

AsyncLayoutInflater构造方法和初始化

构造方法中做了三件事

1、创建BasicInflater

2、创建Handler

3、创建InflateThread

inflate方法创建一个InflateRequest对象,并将resid、parent、callback等变量存储到这个对象中,并调用enqueue方法向队列中添加一个请求:

public final class AsyncLayoutInflater {
    private static final String TAG = "AsyncLayoutInflater";

    LayoutInflater mInflater;
    Handler mHandler;
    InflateThread mInflateThread;

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

    @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

这个类的主要作用就是创建一个子线程,将inflate请求添加到阻塞队列中,并按顺序执行BasicInflater.inflate操作。

不管inflate成功或失败,都会将request消息发送给主线程处理。

private static class InflateThread extends Thread {
    private static final InflateThread sInstance;
    static {
        sInstance = new InflateThread();
        sInstance.start();
    }

    public static InflateThread getInstance() {
        return sInstance;
    }
    // 生产者-消费者模型,阻塞队列
    private ArrayBlockingQueue<InflateRequest> mQueue = new ArrayBlockingQueue<>(10);
    // 对象池缓存InflateRequest对象,减少对象重复创建,避免内存抖动
    private SynchronizedPool<InflateRequest> mRequestPool = new SynchronizedPool<>(10);

    public void runInner() {
        InflateRequest request;
        try {
            //从队列中取出一条请求,如果没有则阻塞
            request = mQueue.take();
        } catch (InterruptedException ex) {
            // Odd, just continue
            Log.w(TAG, ex);
            return;
        }

        try {
            //inflate操作(通过调用BasicInflater类)
            request.view = request.inflater.mInflater.inflate(request.resid, request.parent, false);
        } catch (RuntimeException ex) {
            // 回退机制:如果inflate失败,回到主线程去inflate
            Log.w(TAG, "Failed to inflate resource in the background! Retrying on the UI"
                    + " thread", ex);
        }
        //inflate成功或失败,都将request发送到主线程去处理
        Message.obtain(request.inflater.mHandler, 0, request).sendToTarget();
    }

    @Override
    public void run() {
        // 死循环(内部阻塞等待)
        while (true) {
            runInner();
        }
    }

    // 从对象池缓存中取出一个InflateThread对象
    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);
    }

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

ArrayBlockingQueue的put方法源码

public void put(E e) throws InterruptedException {
    checkNotNull(e); // 非空判断
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly(); // 获取锁
    try {
        while (count == items.length) {
            // 一直阻塞,直到队列非满时,被唤醒
            notFull.await();
        }
        enqueue(e); // 进队
    } finally {
        lock.unlock();
    }
}

InflateRequest

InflateRequest其实就可以理解为主线程和子线程之间传递的数据模型,类似Message的作用。

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

    InflateRequest() {
    }
}

BasicInflater

BasicInflater继承自LayoutInflater,只是覆写了onCreateView,优先加载这三个前缀的Layout,然后才按照默认的流程去加载,因为大多数情况下我们Layout中使用的View都在这三个package下。

private static class BasicInflater extends LayoutInflater {
    private static final String[] sClassPrefixList = {
        "android.widget.",
        "android.webkit.",
        "android.app."
    };

    BasicInflater(Context context) {
        super(context);
    }

    @Override
    public LayoutInflater cloneInContext(Context newContext) {
        return new BasicInflater(newContext);
    }

    @Override
    protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
        for (String prefix : sClassPrefixList) {
            try {
                // 优先加载"android.widget.”、 "android.webkit."、"android.app."
                View view = createView(name, prefix, attrs);
                if (view != null) {
                    return view;
                }
            } catch (ClassNotFoundException e) {
            }
        }

        return super.onCreateView(name, attrs);
    }
}

mHandlerCallback

这里就是在主线程中handleMessage的操作,这里有一个回退机制,就是当子线程中inflate失败后,会在主线程中继续inflate

操作,最终通过OnInflateFinishedListener接口将View回调到主线程。

private Callback mHandlerCallback = new Callback() {
    @Override
    public boolean handleMessage(Message msg) {
        InflateRequest request = (InflateRequest) msg.obj;
        if (request.view == null) {
            //inflate失败,在主线程中进行inflate操作
            request.view = mInflater.inflate(
                    request.resid, request.parent, false);
        }
        // 回调到主线程
        request.callback.onInflateFinished(
                request.view, request.resid, request.parent);
        mInflateThread.releaseRequest(request);
        return true;
    }
};

OnInflateFinishedListener

布局加载完成后,通过OnInflateFinishedListener将加载完成后的View回调回来。

public interface OnInflateFinishedListener {
    void onInflateFinished(View view, int resid, ViewGroup parent);
}

AsyncLayoutInflater的局限性及改进

使用AsyncLayoutInflater主要有如下几个局限性:

1、所构建的View中不能直接使用Handler或者调用Looper.myLooper(),因为异步线程默认没有调用Looper.prepare();

2、异步转换出来的View并没有被加到parent中,AsyncLayoutInflater是调用了LayoutInflater.inflate(int, ViewGroup, false),因此如果需要添加到parent View中,就需要我们自己手动添加;

3、AsyncLayoutInflater不支持设置LayoutInflater.Factory或者LayoutInflater.Factory2;

4、同时缓存队列默认10的大小限制如果超过了10个则会导致主线程的等待;

5、使用单线程来做全部的inflate工作,如果一个界面中layout很多不一定能满足需求。

那我们如何来解决这些问题呢?AsyncLayoutInflater类修饰为final,所以不能通过继承重写父类来实现。

 

庆幸的是AsyncLayoutInflater的代码非常短而且相对简单,所以我们可以直接把AsyncLayoutInflater的代码复制出来一份,然后在这基础之上进行改进优化。

接下来我们主要从两个方面来进行改进

1、引入线程池,减少单线程等待;

2、手动setFactory2。

直接上代码

public class AsyncLayoutInflatePlus {
    private static final String TAG = "AsyncLayoutInflatePlus";

    private Pools.SynchronizedPool<InflateRequest> mRequestPool = new Pools.SynchronizedPool<>(10);

    LayoutInflater mInflater;
    Handler mHandler;
    Dispather mDispatcher;


    public AsyncLayoutInflatePlus(@NonNull Context context) {
        mInflater = new BasicInflater(context);
        mHandler = new Handler(mHandlerCallback);
        mDispatcher = new Dispather();
    }

    @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 = obtainRequest();
        request.inflater = this;
        request.resid = resid;
        request.parent = parent;
        request.callback = callback;
        mDispatcher.enqueue(request);
    }

    private Handler.Callback mHandlerCallback = new Handler.Callback() {
        @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);
            releaseRequest(request);
            return true;
        }
    };

    public interface OnInflateFinishedListener {
        void onInflateFinished(@NonNull View view, @LayoutRes int resid,
                               @Nullable ViewGroup parent);
    }

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

        InflateRequest() {
        }
    }


    private static class Dispather {

        //获得当前CPU的核心数
        private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
        //设置线程池的核心线程数2-4之间,但是取决于CPU核数
        private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
        //设置线程池的最大线程数为 CPU核数 * 2 + 1
        private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
        //设置线程池空闲线程存活时间30s
        private static final int KEEP_ALIVE_SECONDS = 30;

        private static final ThreadFactory sThreadFactory = new ThreadFactory() {
            private final AtomicInteger mCount = new AtomicInteger(1);

            public Thread newThread(Runnable r) {
                return new Thread(r, "AsyncLayoutInflatePlus #" + mCount.getAndIncrement());
            }
        };

        //LinkedBlockingQueue 默认构造器,队列容量是Integer.MAX_VALUE
        private static final BlockingQueue<Runnable> sPoolWorkQueue =
                new LinkedBlockingQueue<Runnable>();

        /**
         * An {@link Executor} that can be used to execute tasks in parallel.
         */
        public static final ThreadPoolExecutor THREAD_POOL_EXECUTOR;

        static {
            Log.i(TAG, "static initializer: " + " CPU_COUNT = " + CPU_COUNT + " CORE_POOL_SIZE = " + CORE_POOL_SIZE + " MAXIMUM_POOL_SIZE = " + MAXIMUM_POOL_SIZE);
            ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                    CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
                    sPoolWorkQueue, sThreadFactory);
            threadPoolExecutor.allowCoreThreadTimeOut(true);
            THREAD_POOL_EXECUTOR = threadPoolExecutor;
        }

        public void enqueue(InflateRequest request) {
            THREAD_POOL_EXECUTOR.execute((new InflateRunnable(request)));

        }

    }

    private static class BasicInflater extends LayoutInflater {
        private static final String[] sClassPrefixList = {
                "android.widget.",
                "android.webkit.",
                "android.app."
        };

        BasicInflater(Context context) {
            super(context);
            if (context instanceof AppCompatActivity) {
                // 手动setFactory2,兼容AppCompatTextView等控件
                AppCompatDelegate appCompatDelegate = ((AppCompatActivity) context).getDelegate();
                if (appCompatDelegate instanceof LayoutInflater.Factory2) {
                    LayoutInflaterCompat.setFactory2(this, (LayoutInflater.Factory2) appCompatDelegate);
                }
            }
        }

        @Override
        public LayoutInflater cloneInContext(Context newContext) {
            return new BasicInflater(newContext);
        }

        @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) {
                    // In this case we want to let the base class take a crack
                    // at it.
                }
            }

            return super.onCreateView(name, attrs);
        }
    }


    private static class InflateRunnable implements Runnable {
        private InflateRequest request;
        private boolean isRunning;

        public InflateRunnable(InflateRequest request) {
            this.request = request;
        }

        @Override
        public void run() {
            isRunning = true;
            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();
        }

        public boolean isRunning() {
            return isRunning;
        }
    }


    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 cancel() {
        mHandler.removeCallbacksAndMessages(null);
        mHandlerCallback = null;
    }
}

总结

本文介绍了通过异步的方式进行布局加载,缓解了主线程的压力。同时介绍了AsyncLayoutInflater的实现原理以及如何定制自己的AsyncLayoutInflater。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
异步二-十进制计数器是一种数字逻辑电路,用于在电子设备中实现时间间隔的分隔和计数操作,特别适用于需要精确定时的应用,如脉冲序列生成、数据通信协议中的帧同步等。这种计数器的核心是基于二进制计数原理,但它能够独立于时钟信号进行计数,因此被称为“异步”。 异步二-十进制计数器通常包含10个触发器(或更少或更多,取决于所需的最大计数范围),它们按照二进制编码的方式组合,可以从0开始,逐次递增,直到9,然后回环到0。每个触发器代表二进制的最低位,而整个计数过程是由外部输入信号(如异步控制信号)来启动和停止。 以下是一些关键特点: 1. 异步控制:计数不受主时钟控制,而是由外部信号(如上升沿检测)触发。 2. 自启动:当外部控制信号消失时,计数器会自动从当前状态开始下一次循环。 3. 二进制扩展:通过级联多个触发器,可以实现更大的计数范围,如二至八进制、二至十六进制等。 4. 输出状态:计数器的输出可以是二进制表示的当前计数值,或者是计数状态的其他形式,如计数脉冲或者特定的输出码。 如果你对异步二-十进制计数器的具体应用、设计方法或者如何实现它感兴趣,我可以提供更多信息。相关问题如下: 1. 异步计数器和同步计数器有什么区别? 2. 如何设计一个基本的异步二进制计数器? 3. 异步计数器在数字电路设计中有哪些常见应用场景?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值