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
以下两种方式都可以获取 view 最新的 DrawingCache
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
view.buildDrawingCache();
Bitmap drawingCache = view.getDrawingCache();
若想更新cache, 必须要调用destoryDrawingCache
方法把旧的cache销毁,才能建立新的
当调用setDrawingCacheEnabled
方法设置为false, 系统也会自动把原来的cache销毁