Bitmap处理之创建可自动回收资源的ImageView
来源:互联网 发布:json返回html标签 编辑:程序博客网 时间:2024/06/06 11:57
转载地址:http://blog.csdn.net/android_learn/article/details/8837234#t5
[-]
- 创建可自动回收资源的BitmapDrawable
- 判断bitmap是否还存在并且可用
- 判断bitmap的状态
- 判断bitmap是否已经cache
- 判断bitmap是否已经显示
- 完整的代码
- 创建可自动回收资源的ImageView
1.创建可自动回收资源的BitmapDrawable
继承BitmapDrawable,设置标志位 mCacheRefCount cache计数,mDisplayRefCount 显示计数,mHasBeenDisplayed判断是否已经显示过了具体的code:
- private int mCacheRefCount = 0; //cache计数
- private int mDisplayRefCount = 0; //显示计数
- private boolean mHasBeenDisplayed;//是否已经显示过
判断bitmap是否还存在并且可用
- private synchronized boolean hasValidBitmap() {
- Bitmap bitmap = getBitmap();
- return bitmap != null && !bitmap.isRecycled();
- }
判断bitmap的状态
- private synchronized void checkState() {
- // If the drawable cache and display ref counts = 0, and this drawable
- // has been displayed, then recycle
- if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed
- && hasValidBitmap()) {
- getBitmap().recycle();
- }
- }
通过cache计数,显示计数,是否已经显示,以及bitmap是否还存在,如果四个条件都满足,就显式的回收bitmap资源
判断bitmap是否已经cache
- public void setIsCached(boolean isCached) {
- synchronized (this) {
- if (isCached) {
- mCacheRefCount++;
- } else {
- mCacheRefCount--;
- }
- }
- // Check to see if recycle() can be called
- checkState();
- }
调用的地方是,如果已经将某个bitmap加入了cache(memory cache,disk cache)setIsCached(true)这时cache计数就会增加1,如果某个bitmap从cache中删除,就setIsCached(false)这时cache计数就减去1,然后调用checkState()方法,来判断bitmap是否需要回收
判断bitmap是否已经显示
- public void setIsDisplayed(boolean isDisplayed) {
- synchronized (this) {
- if (isDisplayed) {
- mDisplayRefCount++;
- mHasBeenDisplayed = true;
- } else {
- mDisplayRefCount--;
- }
- }
- // Check to see if recycle() can be called
- checkState();
- }
完整的代码
- public class RecyclingBitmapDrawable extends BitmapDrawable {
- static final String LOG_TAG = "CountingBitmapDrawable";
- private int mCacheRefCount = 0;
- private int mDisplayRefCount = 0;
- private boolean mHasBeenDisplayed;
- public RecyclingBitmapDrawable(Resources res, Bitmap bitmap) {
- super(res, bitmap);
- }
- /**
- * Notify the drawable that the displayed state has changed. Internally a
- * count is kept so that the drawable knows when it is no longer being
- * displayed.
- *
- * @param isDisplayed - Whether the drawable is being displayed or not
- */
- public void setIsDisplayed(boolean isDisplayed) {
- synchronized (this) {
- if (isDisplayed) {
- mDisplayRefCount++;
- mHasBeenDisplayed = true;
- } else {
- mDisplayRefCount--;
- }
- }
- // Check to see if recycle() can be called
- checkState();
- }
- /**
- * Notify the drawable that the cache state has changed. Internally a count
- * is kept so that the drawable knows when it is no longer being cached.
- *
- * @param isCached - Whether the drawable is being cached or not
- */
- public void setIsCached(boolean isCached) {
- synchronized (this) {
- if (isCached) {
- mCacheRefCount++;
- } else {
- mCacheRefCount--;
- }
- }
- // Check to see if recycle() can be called
- checkState();
- }
- private synchronized void checkState() {
- // If the drawable cache and display ref counts = 0, and this drawable
- // has been displayed, then recycle
- if (mCacheRefCount <= 0 && mDisplayRefCount <= 0 && mHasBeenDisplayed
- && hasValidBitmap()) {
- if (BuildConfig.DEBUG) {
- Log.d(LOG_TAG, "No longer being used or cached so recycling. "
- + toString());
- }
- getBitmap().recycle();
- }
- }
- private synchronized boolean hasValidBitmap() {
- Bitmap bitmap = getBitmap();
- return bitmap != null && !bitmap.isRecycled();
- }
- }
2.创建可自动回收资源的ImageView
基于RecyclingBitmapDrawable来实现 RecyclingImageView
具体实现
重载Imageiew的setImageDrawable()方法
- public void setImageDrawable(Drawable drawable) {
- // Keep hold of previous Drawable
- final Drawable previousDrawable = getDrawable();
- // Call super to set new Drawable
- super.setImageDrawable(drawable);
- // Notify new Drawable that it is being displayed
- notifyDrawable(drawable, true);
- // Notify old Drawable so it is no longer being displayed
- notifyDrawable(previousDrawable, false);
- }
- private static void notifyDrawable(Drawable drawable, final boolean isDisplayed) {
- if (drawable instanceof RecyclingBitmapDrawable) {
- // The drawable is a CountingBitmapDrawable, so notify it
- ((RecyclingBitmapDrawable) drawable).setIsDisplayed(isDisplayed);
- } else if (drawable instanceof LayerDrawable) {
- // The drawable is a LayerDrawable, so recurse on each layer
- LayerDrawable layerDrawable = (LayerDrawable) drawable;
- for (int i = 0, z = layerDrawable.getNumberOfLayers(); i < z; i++) {
- notifyDrawable(layerDrawable.getDrawable(i), isDisplayed);
- }
- }
- }
http://developer.android.com/training/displaying-bitmaps/process-bitmap.html#concurrency
0 0
- Bitmap处理之创建可自动回收资源的ImageView
- Bitmap处理之创建可自动回收资源的ImageView
- 强制回收ImageView的bitmap
- 手动回收ImageVIew的图片资源
- bitmap资源回收问题
- 创建分离线程-自动回收资源
- 可耗竭且不可回收的资源
- Android OOM ,回收布局文件中ImageView占用的内存.Bitmap OOM回收解决.
- [Android Memory] 手动回收ImageVIew的图片资源
- 自动回收资源的单例模式
- ImageView,Bitmap的浅析
- .NET对象的创建、垃圾回收、非托管资源的手动处理
- Bitmap 处理 之加载大的Bitmap
- 回收bitmap后的ImageView,activity死前的回调方法
- Bitmap的回收
- 创建bitmap时的优化处理
- Android回收图片的bitmap,导致的Canvas: trying to use a recycled bitmap异常处理
- 可移动的ImageView
- 数据包离开主机的过程~~~
- hive sql 优化
- VC玩转.INI文件
- android-adapter小结
- 错觉思维
- Bitmap处理之创建可自动回收资源的ImageView
- Java基本功练习十九(抽象类和接口【抽象类、接口(监听器、克隆、可比较)、包装类、有理数类实现】)
- 158 鄭光弼 守夫
- mirrorlink相关参考网址
- VC中List Control简单用法
- Tomcat启动报“严重: Error listenerStart”错误的解决办法
- Cocos2d-x碰撞检查与消灭的实现
- 《图灵和ACM图灵奖》阅读笔记
- 重构笔记——分解临时变量