安卓View的缓冲机制

View组件显示的内容能够通过cache机制保存为bitmap, 主要有下面方法:

void  setDrawingCacheEnabled(boolean flag), 

Bitmap  getDrawingCache(boolean autoScale), 

void  buildDrawingCache(boolean autoScale), 

void  destroyDrawingCache()

我们要获取cache首先要通过setDrawingCacheEnable方法开启cache,然后再调用getDrawingCache方法就能够获得view的cache图片了。

buildDrawingCache方法能够不用调用。由于调用getDrawingCache方法时,若果cache没有建立,系统会自己主动调用buildDrawingCache方法生成cache。

若想更新cache, 必需要调用destoryDrawingCache方法把旧的cache销毁。才干建立新的。

当调用setDrawingCacheEnabled方法设置为false, 系统也会自己主动把原来的cache销毁。

   
另外,ViewGroup在绘制子view时,也提供了两个方法

void setChildrenDrawingCacheEnabled(boolean enabled) 

setChildrenDrawnWithCacheEnabled(boolean enabled) 

setChildrenDrawingCacheEnabled方法能够使viewgroup里全部的子view开启cache;

setChildrenDrawnWithCacheEnabled使在绘制子view时。若该子view开启了cache, 则使用它的cache进行绘制。从而节省绘制时间。

获取cache一般会占用一定的内存,所以通常不须要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。
原文地址:https://www.cnblogs.com/llguanli/p/7101841.html