Skip to content

Commit

Permalink
expose bitmap pool via ImageManager
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Nov 5, 2013
1 parent 8344b14 commit 0d1005f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions library/src/com/bumptech/glide/resize/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ImageManager {

private final BitmapReferenceCounter bitmapReferenceCounter;
private final int bitmapCompressQuality;
private final BitmapPool bitmapPool;
private boolean shutdown = false;

private final Handler mainHandler = new Handler();
Expand Down Expand Up @@ -363,6 +364,7 @@ private ImageManager(Builder builder) {
memoryCache = builder.memoryCache;
diskCache = builder.diskCache;
bitmapReferenceCounter = builder.bitmapReferenceCounter;
bitmapPool = builder.bitmapPool;
resizer = new ImageResizer(builder.bitmapPool, builder.decodeBitmapOptions);

memoryCache.setImageRemovedListener(new MemoryCache.ImageRemovedListener() {
Expand All @@ -373,6 +375,22 @@ public void onImageRemoved(Bitmap removed) {
});
}

/**
* Get the {@link BitmapPool} this ImageManager is using. If Bitmap recycling is not supported, an
* {@link BitmapPoolAdapter} will be returned. For the pool to be useful you must return a bitmap to the pool for
* every bitmap you obtain from the pool.
*
* <p>
* Note the BitmapPool api is likely to change in the near future to support some new features released in
* KitKat.
* </p>
*
* @return The bitmap pool.
*/
public BitmapPool getBitmapPool() {
return bitmapPool;
}

/**
* Load an image
*
Expand Down
14 changes: 13 additions & 1 deletion library/tests/src/com/bumptech/glide/GlideTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bumptech.glide;

import android.graphics.Bitmap;
import android.net.Uri;
import android.test.ActivityTestCase;
import android.view.ViewGroup;
Expand All @@ -8,6 +9,7 @@
import com.bumptech.glide.loader.stream.StreamLoader;
import com.bumptech.glide.presenter.ImagePresenter;
import com.bumptech.glide.presenter.target.ImageViewTarget;
import com.bumptech.glide.resize.bitmap_recycle.BitmapPool;
import com.bumptech.glide.tests.R;

import java.io.File;
Expand Down Expand Up @@ -220,6 +222,17 @@ public void testClearingTagReplacesPresenter() {
Glide.load("b").into(imageView);
}

public void testObtainAndOfferToBitmapPool() {
Bitmap small = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Bitmap large = Bitmap.createBitmap(512, 512, Bitmap.Config.RGB_565);
BitmapPool bitmapPool = Glide.get().getImageManager(getInstrumentation().getContext()).getBitmapPool();
bitmapPool.put(small);
bitmapPool.put(large);

assertEquals(small, bitmapPool.get(small.getWidth(), small.getHeight(), small.getConfig()));
assertEquals(large, bitmapPool.get(large.getWidth(), large.getHeight(), large.getConfig()));
}

private void assertDifferentPresenters(Glide.Request a, Glide.Request b) {
a.into(imageView);
ImagePresenter first = getImagePresenterFromView();
Expand All @@ -233,5 +246,4 @@ private void assertDifferentPresenters(Glide.Request a, Glide.Request b) {
assertSame(first, second);
assertNotSame(first, third);
}

}

0 comments on commit 0d1005f

Please sign in to comment.