高效的显示位图(五):管理位图内…

- 除了缓冲机制,还有其它措施可以用来为垃圾回收和位图重用增加便利
- 针对不同版本:
  • Android 2.2及以前版本,当垃圾回收启动,应用中的线程全部停止,这导致性能损失,Android 2.3.3引入并发垃圾回收机制
  • Android 2.3.3及更早版本,像素数据存储在本地内存,与为图对象(存储于虚拟机堆)本身隔离。本地内存数据无法以可以预知的方式释放,致使程序可能超过内存限制而崩溃。Android 3.0以后,像素数据也存储于虚拟机堆
- 本篇展示如何根据不同版本优化位图内存管理

Android 2.3.3以下版本内存管理
- recycle()方法:允许应用尽快回收内存
private int mCacheRefCount = 0;
private int mDisplayRefCount = 0;
...
// Notify the drawable that the displayed state has changed.
// Keep a count to determine when the drawable is no longer displayed.
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.
// Keep a count to determine when the drawable is no longer being cached.
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()) {
        getBitmap
().recycle();
   
}
}

private synchronized boolean hasValidBitmap() {
   
Bitmap bitmap = getBitmap();
   
return bitmap != null && !bitmap.isRecycled();
}

Android 3.0以上版本内存管理
- BitmapFactory.Options.inBitmap:解码器将尝试重用已经存在的位图对象:
  • 被重用的位图对象必须与源内容大小一致,并且是JPG或PNG格式
  • 被重用的位图的configuration将覆盖inPreferredConfig设置,如果有的话
  • 你应该使用解码方法返回的位图对象。被重用的位图不一定还能用
* 保存一个位图待用:
- 如何保存一个已经存在的位图
HashSet<SoftReference<Bitmap>> mReusableBitmaps;
private LruCache<String, BitmapDrawable> mMemoryCache;

// If you're running on Honeycomb or newer, create
// a HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    mReusableBitmaps
= new HashSet<SoftReference<Bitmap>>();
}

mMemoryCache
= new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) {

   
// Notify the removed entry that is no longer being cached.
   
@Override
   
protected void entryRemoved(boolean evicted, String key,
           
BitmapDrawable oldValue, BitmapDrawable newValue) {
       
if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
           
// The removed entry is a recycling drawable, so notify it
           
// that it has been removed from the memory cache.
           
((RecyclingBitmapDrawable) oldValue).setIsCached(false);
       
} else {
           
// The removed entry is a standard BitmapDrawable.
           
if (Utils.hasHoneycomb()) {
               
// We're running on Honeycomb or later, so add the bitmap
               
// to a SoftReference set for possible use with inBitmap later.
                mReusableBitmaps
.add
                       
(new SoftReference<Bitmap>(oldValue.getBitmap()));
           
}
       
}
   
}
....
}

* 使用现有的位图:
- 解码方法检查是否有现存的位图可用:
public static Bitmap decodeSampledBitmapFromFile(String filename,
       
int reqWidth, int reqHeight, ImageCache cache) {

   
final BitmapFactory.Options options = new BitmapFactory.Options();
   
...
   
BitmapFactory.decodeFile(filename, options);
   
...

   
// If we're running on Honeycomb or newer, try to use inBitmap.
   
if (Utils.hasHoneycomb()) {
        addInBitmapOptions
(options, cache);
   
}
   
...
   
return BitmapFactory.decodeFile(filename, options);
}
- addInBitmapOptions():
private static void addInBitmapOptions(BitmapFactory.Options options,
       
ImageCache cache) {
   
// inBitmap only works with mutable bitmaps, so force the decoder to
   
// return mutable bitmaps.
    options
.inMutable = true;

   
if (cache != null) {
       
// Try to find a bitmap to use for inBitmap.
       
Bitmap inBitmap = cache.getBitmapFromReusableSet(options);

       
if (inBitmap != null) {
           
// If a suitable bitmap has been found, set it as the value of
           
// inBitmap.
            options
.inBitmap = inBitmap;
       
}
   
}
}

// This method iterates through the reusable bitmaps, looking for one
// to use for inBitmap:
protected Bitmap getBitmapFromReusableSet(BitmapFactory.Options options) {
       
Bitmap bitmap = null;

   
if (mReusableBitmaps != null && !mReusableBitmaps.isEmpty()) {
       
final Iterator<SoftReference<Bitmap>> iterator
               
= mReusableBitmaps.iterator();
       
Bitmap item;

       
while (iterator.hasNext()) {
            item
= iterator.next().get();

           
if (null != item && item.isMutable()) {
               
// Check to see it the item can be used for inBitmap.
               
if (canUseForInBitmap(item, options)) {
                    bitmap
= item;

                   
// Remove from reusable set so it can't be used again.
                    iterator
.remove();
                   
break;
               
}
           
} else {
               
// Remove from the set if the reference has been cleared.
                iterator
.remove();
           
}
       
}
   
}
   
return bitmap;
}
- 最后,此方法检查找到的位图对象是否可用:
private static boolean canUseForInBitmap(
       
Bitmap candidate, BitmapFactory.Options targetOptions) {
   
int width = targetOptions.outWidth / targetOptions.inSampleSize;
   
int height = targetOptions.outHeight / targetOptions.inSampleSize;

   
// Returns true if "candidate" can be used for inBitmap re-use with
   
// "targetOptions".
   
return candidate.getWidth() == width && candidate.getHeight() == height;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值