做项目的过程中,发现一种一种优化view inflate耗时的方法,思路是预先将view inflate出来备用,用的时候直接取出来,同时再生成下一个(要考虑屏幕方向)。代码如下所示: <script src="https://gist.github.com/legendmohe/8e0d56e2c7d772c12753ca66a80020bc.js"></script> 用法大概如下所述: 1. 在适当的地方调用push方法。 2. 在使用view的时候,使用类似如下的代码:View view = ViewCache.getInstance().popAndAquire(getResources().getConfiguration()); if (view != null) { long ts = SystemClock.uptimeMillis(); // 得到view Log.d(TAG, "onCreateView: load view from ViewCache:" + (SystemClock.uptimeMillis() - ts)); } else { long ts = SystemClock.uptimeMillis(); // 手动inflater,如Inflater.inflate() Log.d(TAG, "onCreateView: load view from inflater:" + (SystemClock.uptimeMillis() - ts)); }
查看原文:http://legendmohe.net/2017/12/15/android-%e4%b8%80%e7%a7%8d%e4%bc%98%e5%8c%96view-inflate%e8%80%97%e6%97%b6%e7%9a%84%e6%96%b9%e6%b3%95/
[Android] 一种优化view inflate耗时的方法
最新推荐文章于 2022-09-09 10:32:03 发布