2012-05-29 102 views
3

在我的应用程序中,我想交换图像当用户点击它时在运行时。删除绘图缓存

有两个imageviews当用户点击第一个图像,然后在我取第一的ImageView图像的位图,并分配给第二imageview的此同一时间,我用点击第二图像下面的代码:

public Bitmap createBitmap(ImageView imageview) { 
    imageview.setDrawingCacheEnabled(true); 
    imageview.buildDrawingCache(false); 

    if(imageview.getDrawingCache() != null) { 
     Bitmap bitmap = Bitmap.createBitmap(imageview.getDrawingCache()); 
     imageview.setDrawingCacheEnabled(false); 
     return bitmap; 
    } else { 
     return null; 
    } 
} 

代码工作正常,但缓存不清除每次和位图用前一次缓存创建,所以我怎么可以清除位图缓存?

+0

plz使用这个你会得到答https://github.com/thest1/LazyList – himanshu

+0

请选择你的答案 – breceivemail

回答

2

这是一个例子,例如我在哪里使用Free the native object associated with this bitmap

Bitmap bitmap; 

public Bitmap createBitmap(ImageView imageview) { 
    if (bitmap != null) { 
     bitmap.recycle(); 
     bitmap = null; 
    } 
    bitmap = Bitmap.createBitmap(imageview.getDrawingCache()); 
    // Your Code of bitmap Follows here 
} 

在使用Bitmap之前,只需释放该对象。

+0

thnx的答复...但我想我必须回收位图之前把价值,纠正我,如果我错了 – Prachi

+0

@curious_mind:在适当的时候分配位图时作出的更改。 – Bhavin

+0

蜂巢+位图不再分配在本机堆中,它们现在在Dalvik堆中。调用recycle()不再需要。 –

1

使用bitmap.recycle();评估您的位图之前清除它们的缓存,然后重新创建它。