Android笔记--Bitmap(三) 针对不用Android版本的位图管理

Bitmap(三) | Android不同版本的相应操作

在不同的Android版本中。位图的存储方式是不同的。

1.小于等于 Android 2.2 (API level 8)

垃圾收集器回收内存时会使所有线程停止,这回严重影响性能。Android2.3之后,加入了并行的垃圾回收。这就让不再被引用的位图可以直接被回收。

2.Android 2.3.3 (API level 10) and lower

这种版本的设备,位图的像素数据存储在native的内存中。而bitmap对象在VM的堆内存中。他和bitmap是分离的。而在native内存存储的像素数据不具有可预知性,所以桐城会导致应用内存溢出或者崩溃。

3.Android 3.0 (API level 11) through Android 7.1 (API level 25)
像素数据和bitmap都存储在VM的堆内存上。

4.Android 8.0 (API level 26), and higher,

像素数据存在native堆上

所以对于不同的版本要做不同的位图管理。

1. <= Android 2.3.3(API10)版本的位图处理

1.需要显示调用recycle()方法

private int mCacheRefCount = 0;
private int mDisplayRefCount = 0;//这两个变量负责计数
//只有当界面既没有引用位图。缓存中也没有保存位图且没有被界面展示,且bitmap还存在的时候才允许调用recycle()
...
// 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();
}

2. >= Android 3.0 (API level 11) 版本的位图管理

Android3.0以后引入了 BitmapFactory.Options.inBitmap变量。如果设置了这一项,那decodeXXX方法们在用这个Options加载位图时将尝试重用现有位图。这意味着位图的内存被重用,从而提高了性能,同时消除了内存分配和回收的过程。

但是 inBitmap得使用时有限制的,特别是在Android 4.4(API Level 19)之前,只有同等大小的位图支持重复利用

a 保存bitmap供后续使用

下面的代码片段演示了如何存储现有位图,以便以后在示例应用程序中使用。当一个应用程序在Android 3。0或更高的运行,而且位图从LruCache剔除出来了,那位图软引用被放置在一个HashSet中,预备后来inbitmap可能重用。

Set<SoftReference<Bitmap>> mReusableBitmaps;
private LruCache<String, BitmapDrawable> mMemoryCache;

// If you're running on Honeycomb or newer, create a
// synchronized HashSet of references to reusable bitmaps.
if (Utils.hasHoneycomb()) {
    mReusableBitmaps =
            Collections.synchronizedSet(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()));
            }
        }
    }
....
}

b 使用一个已经存在的位图

在解压位图的时候如果是Honeycomb或者高版本就试着使用inBitmap

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);
}

inBitmap只适用于不可变位图。

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()) {
        synchronized (mReusableBitmaps) {
            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;
}

最后这个方法决定是否有合适的位图满足需要的大小

注意:4.4版本之后只要是可重用的位图大于等于需要的位图大小就可以重用了。所以建议针对4.4版本做这个重用操作

static boolean canUseForInBitmap(
        Bitmap candidate, BitmapFactory.Options targetOptions) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        int width = targetOptions.outWidth / targetOptions.inSampleSize;
        int height = targetOptions.outHeight / targetOptions.inSampleSize;
        int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= candidate.getAllocationByteCount();
    }

    // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
    return candidate.getWidth() == targetOptions.outWidth
            && candidate.getHeight() == targetOptions.outHeight
            && targetOptions.inSampleSize == 1;
}

/**
 * A helper function to return the byte usage per pixel of a bitmap based on its configuration.
 */
static int getBytesPerPixel(Config config) {
    if (config == Config.ARGB_8888) {
        return 4;
    } else if (config == Config.RGB_565) {
        return 2;
    } else if (config == Config.ARGB_4444) {
        return 2;
    } else if (config == Config.ALPHA_8) {
        return 1;
    }
    return 1;
}
原文地址:https://www.cnblogs.com/zharma/p/8330816.html