View组件的cache机制:
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有
void setDrawingCacheEnabled(booleanflag),
BitmapgetDrawingCache(boolean autoScale),
我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,然后再调用getDrawingCache方法就可以获得view的cache图片了。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。
获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。
示例代码:
currentView.clearFocus();//currentView表示设置的View对象
currentView.setPressed(false);
currentView.setDrawingCacheBackgroundColor(0);
currentView.setDrawingCacheEnabled(true);
Bitmap viewBitmap = currentView.getDrawingCache();
currentView.setDrawingCacheEnabled(false);
原文来自:http://blog.youkuaiyun.com/dylancao/article/details/7661025
View组件显示的内容可以通过cache机制保存为bitmap, 使用到的api有
void setDrawingCacheEnabled(booleanflag),
BitmapgetDrawingCache(boolean autoScale),
我们要获取它的cache先要通过setDrawingCacheEnable方法把cache开启,然后再调用getDrawingCache方法就可以获得view的cache图片了。buildDrawingCache方法可以不用调用,因为调用getDrawingCache方法时,若果cache没有建立,系统会自动调用buildDrawingCache方法生成cache。若果要更新cache, 必须要调用destoryDrawingCache方法把旧的cache销毁,才能建立新的。
当调用setDrawingCacheEnabled方法设置为false, 系统也会自动把原来的cache销毁。
获取cache通常会占用一定的内存,所以通常不需要的时候有必要对其进行清理,通过destroyDrawingCache或setDrawingCacheEnabled(false)实现。
示例代码:
currentView.clearFocus();//currentView表示设置的View对象
currentView.setPressed(false);
currentView.setDrawingCacheBackgroundColor(0);
currentView.setDrawingCacheEnabled(true);
Bitmap viewBitmap = currentView.getDrawingCache();
currentView.setDrawingCacheEnabled(false);
原文来自:http://blog.youkuaiyun.com/dylancao/article/details/7661025
本文详细介绍了Android中View组件的缓存机制,包括如何通过API启用和获取缓存、更新缓存的方法以及释放缓存资源的重要性。通过示例代码展示了具体的实现步骤。
1414

被折叠的 条评论
为什么被折叠?



