1
概述
目錄
前言
在Android佈局優化(一)從佈局加載原理說起中我們說到了佈局加載的兩大性能瓶頸,通過IO操作將XML加載到內存中並進行解析和通過反射創建View。
https://www.jianshu.com/p/8ca35e86d476
當xml文件過大或頁面文件過深,佈局的加載就會較爲耗時。
我們知道,當主線程進行一些耗時操作可能就會導致頁面卡頓,更嚴重的可能會產生ANR,所以我們能如何來進行佈局加載優化呢?
解決這個問題有兩種思路,直接解決和側面緩解。
直接解決就是不使用IO和反射等技術(這個我們會在下一節進行介紹)。
側面緩解的就是既然耗時操作難以避免,那我們能不能把耗時操作放在子線程中,等到inflate操作完成後再將結果回調到主線程呢?
答案當然是可以的,Android爲我們提供了AsyncLayoutInflater類來進行異步佈局加載
2
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_right);
rv.setLayoutManager(new V7LinearLayoutManager(MainActivity.this));
rv.setAdapter(new RightRvAdapter(MainActivity.this));
}
});
}
3
AsyncLayoutInflater源碼分析
AsyncLayoutInflater的源碼非常短,也比較容易理解,總共只有170行左右。
AsyncLayoutInflater構造方法和初始化
構造方法中做了三件事件
-
創建BasicInflater
-
創建Handler
-
創建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操作(BasicInflater實際上就是LayoutInflater的子類)。
不管infalte成功或失敗後,都會將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);
//使用了對象池來緩存InflateThread對象,減少對象重複多次創建,避免內存抖動
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);
}
}
}
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) {
//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);
}
4
AsyncLayoutInflater的侷限性及改進
使用AsyncLayoutInflate主要有如下幾個侷限性:
-
所有構建的View中必須不能直接使用 Handler 或者是調用 Looper.myLooper(),因爲異步線程默認沒有調用 Looper.prepare ()
-
異步轉換出來的 View 並沒有被加到 parent view中,AsyncLayoutInflater 是調用了 LayoutInflater.inflate(int, ViewGroup, false),因此如果需要加到 parent view 中,就需要我們自己手動添加;
-
AsyncLayoutInflater 不支持設置 LayoutInflater.Factory 或者 LayoutInflater.Factory2
-
同時緩存隊列默認 10 的大小限制如果超過了10個則會導致主線程的等待
-
使用單線程來做全部的 inflate 工作,如果一個界面中 layout 很多不一定能滿足需求
那我們如何來解決這些問題呢?AsyncLayoutInflate類修飾爲 final ,所以不能通過繼承重寫父類來實現。
慶幸的是AsyncLayoutInflate的代碼非常短而且相對簡單,所以我們可以直接把AsyncLayoutInflate的代碼複製出來一份,然後在這基礎之上進行改進優化。
接下來我們主要從兩個方面來進行優化
-
引入線程池,減少單線程等待
-
手動設置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;
}
}
總結
本文介紹了通過異步的方式進行佈局加載,緩解了主線程的壓力。同時也介紹了AsyncLayoutInflate的實現原理以及如何定製自己的AsyncLayoutInflate。
本文的定製方式僅僅只是作爲一個參考,具體的實現方式可以根據自己項目的實際情況來定製。