如需轉載請評論或簡信,并注明出處,未經允許不得轉載
系列文章
- Android布局優化(一)LayoutInflate — 從布局加載原理說起
- Android布局優化(二)優雅獲取界面布局耗時
- Android布局優化(三)使用AsyncLayoutInflater異步加載布局
- Android布局優化(四)X2C — 提升布局加載速度200%
- Android布局優化(五)繪制優化—避免過度繪制
目錄
前言
在Android布局優化(一)從布局加載原理說起中我們說到了布局加載的兩大性能瓶頸,通過IO操作將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_right);
rv.setLayoutManager(new V7LinearLayoutManager(MainActivity.this));
rv.setAdapter(new RightRvAdapter(MainActivity.this));
}
});
}
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);
}
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。本文的定制方式僅僅只是作為一個參考,具體的實現方式可以根據自己項目的實際情況來定制