summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Wei <markwei@google.com>2013-10-22 16:57:01 -0700
committerMark Wei <markwei@google.com>2013-10-22 17:06:30 -0700
commita23358fbfd62c7aa1c84bfa8395b4dc427a71ce6 (patch)
tree5fe5ce44556e0d8dc267c168c6266651ff8a0222 /src
parent333647f2886289a23a09a84365aa42d328ec54dc (diff)
downloadbitmap-a23358fbfd62c7aa1c84bfa8395b4dc427a71ce6.tar.gz
Add BasicImageView to unbind on detach for clients with long-lived caches.
Change visibility of mBitmap back to private. Expose through getter. Bug: 11337575 Change-Id: I511976388cc3b6962434416d9e7a632e6d8a1b72
Diffstat (limited to 'src')
-rw-r--r--src/com/android/bitmap/drawable/BasicBitmapDrawable.java12
-rw-r--r--src/com/android/bitmap/view/BasicImageView.java78
2 files changed, 89 insertions, 1 deletions
diff --git a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
index 9cee3c0..b172373 100644
--- a/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
+++ b/src/com/android/bitmap/drawable/BasicBitmapDrawable.java
@@ -34,6 +34,7 @@ import com.android.bitmap.ReusableBitmap;
import com.android.bitmap.util.BitmapUtils;
import com.android.bitmap.util.RectUtils;
import com.android.bitmap.util.Trace;
+import com.android.bitmap.view.BasicImageView;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
@@ -45,18 +46,23 @@ import java.util.concurrent.TimeUnit;
* including request creation/cancelling, and data unbinding and re-binding.
* <p>
* The actual bitmap decode work is handled by {@link DecodeTask}.
+ * <p>
+ * If being used with a long-lived cache (static cache, attached to the Application instead of the
+ * Activity, etc) then make sure to call {@link BasicBitmapDrawable#unbind()} at the appropriate
+ * times so the cache has accurate unref counts. The {@link BasicImageView} class has been created
+ * to do the appropriate unbind operation when the view is detached from the window.
*/
public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCallback,
Drawable.Callback {
protected static Rect sRect;
protected RequestKey mCurrKey;
- protected ReusableBitmap mBitmap;
protected final Paint mPaint = new Paint();
private final BitmapCache mCache;
private final boolean mLimitDensity;
private final float mDensity;
+ private ReusableBitmap mBitmap;
private DecodeTask mTask;
private int mDecodeWidth;
private int mDecodeHeight;
@@ -229,6 +235,10 @@ public class BasicBitmapDrawable extends Drawable implements DecodeTask.DecodeCa
@Override
public void onDecodeCancel(final RequestKey key) { }
+ protected ReusableBitmap getBitmap() {
+ return mBitmap;
+ }
+
private void setBitmap(ReusableBitmap bmp) {
if (mBitmap != null && mBitmap != bmp) {
mBitmap.releaseReference();
diff --git a/src/com/android/bitmap/view/BasicImageView.java b/src/com/android/bitmap/view/BasicImageView.java
new file mode 100644
index 0000000..eaaf953
--- /dev/null
+++ b/src/com/android/bitmap/view/BasicImageView.java
@@ -0,0 +1,78 @@
+package com.android.bitmap.view;
+
+import android.content.Context;
+import android.graphics.Bitmap;
+import android.graphics.drawable.Drawable;
+import android.net.Uri;
+import android.util.AttributeSet;
+import android.widget.ImageView;
+
+import com.android.bitmap.drawable.BasicBitmapDrawable;
+
+/**
+ * A helpful ImageView replacement that can generally be used in lieu of ImageView.
+ * BasicImageView has logic to unbind its BasicBitmapDrawable when it is detached from the window.
+ */
+public class BasicImageView extends ImageView {
+ private BasicBitmapDrawable mDrawable;
+
+ public BasicImageView(final Context context) {
+ this(context, null);
+ }
+
+ public BasicImageView(final Context context, final AttributeSet attrs) {
+ this(context, attrs, 0);
+ }
+
+ public BasicImageView(final Context context, final AttributeSet attrs, final int defStyle) {
+ super(context, attrs, defStyle);
+ }
+
+ /**
+ * Set the given BasicBitmapDrawable as the source for this BasicImageView.
+ * @param drawable The source drawable.
+ */
+ public void setDrawable(BasicBitmapDrawable drawable) {
+ super.setImageDrawable(drawable);
+ mDrawable = drawable;
+ }
+
+ public BasicBitmapDrawable getDrawable() {
+ return mDrawable;
+ }
+
+ @Override
+ protected void onDetachedFromWindow() {
+ super.onDetachedFromWindow();
+
+ mDrawable.unbind();
+ }
+
+ @Override
+ public void setImageDrawable(final Drawable drawable) {
+ throw new UnsupportedOperationException(
+ "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() "
+ + "instead.");
+ }
+
+ @Override
+ public void setImageResource(final int resId) {
+ throw new UnsupportedOperationException(
+ "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() "
+ + "instead.");
+ }
+
+ @Override
+ public void setImageURI(final Uri uri) {
+ throw new UnsupportedOperationException(
+ "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() "
+ + "instead.");
+ }
+
+ @Override
+ public void setImageBitmap(final Bitmap bm) {
+ throw new UnsupportedOperationException(
+ "BasicImageView is only compatible with BasicBitmapDrawable. Use setDrawable() "
+ + "instead.");
+ }
+}