summaryrefslogtreecommitdiff
path: root/src/com/android/bitmap/view/BitmapDrawableImageView.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/android/bitmap/view/BitmapDrawableImageView.java')
-rw-r--r--src/com/android/bitmap/view/BitmapDrawableImageView.java27
1 files changed, 22 insertions, 5 deletions
diff --git a/src/com/android/bitmap/view/BitmapDrawableImageView.java b/src/com/android/bitmap/view/BitmapDrawableImageView.java
index 9a88f3f..5cbc58d 100644
--- a/src/com/android/bitmap/view/BitmapDrawableImageView.java
+++ b/src/com/android/bitmap/view/BitmapDrawableImageView.java
@@ -30,6 +30,11 @@ import com.android.bitmap.drawable.BasicBitmapDrawable;
* A helpful ImageView replacement that can generally be used in lieu of ImageView.
* BitmapDrawableImageView has logic to unbind its BasicBitmapDrawable when it is detached from the
* window.
+ *
+ * If you are using this with RecyclerView,
+ * or any use-case where {@link android.view.View#onDetachedFromWindow} is
+ * not a good signal for unbind,
+ * makes sure you {@link #setShouldUnbindOnDetachFromWindow} to false.
*/
public class BitmapDrawableImageView extends ImageView {
private static final boolean HAS_TRANSIENT_STATE_SUPPORTED =
@@ -38,6 +43,7 @@ public class BitmapDrawableImageView extends ImageView {
private static final boolean PERMANENT = !TEMPORARY;
private BasicBitmapDrawable mDrawable;
+ private boolean mShouldUnbindOnDetachFromWindow = true;
private boolean mAttachedToWindow;
public BitmapDrawableImageView(final Context context) {
@@ -53,6 +59,14 @@ public class BitmapDrawableImageView extends ImageView {
super(context, attrs, defStyle);
}
+ public boolean shouldUnbindOnDetachFromWindow() {
+ return mShouldUnbindOnDetachFromWindow;
+ }
+
+ public void setShouldUnbindOnDetachFromWindow(boolean shouldUnbindOnDetachFromWindow) {
+ mShouldUnbindOnDetachFromWindow = shouldUnbindOnDetachFromWindow;
+ }
+
/**
* Get the source drawable for this BitmapDrawableImageView.
* @return The source drawable casted to the given type, or null if the type does not match.
@@ -72,11 +86,13 @@ public class BitmapDrawableImageView extends ImageView {
*/
public <E extends BasicBitmapDrawable> void setTypedDrawable(E drawable) {
super.setImageDrawable(drawable);
- unbindDrawable();
+ if (drawable != mDrawable) {
+ unbindDrawable();
+ }
mDrawable = drawable;
}
- private void unbindDrawable() {
+ public void unbindDrawable() {
unbindDrawable(PERMANENT);
}
@@ -119,7 +135,7 @@ public class BitmapDrawableImageView extends ImageView {
super.onAttachedToWindow();
mAttachedToWindow = true;
if (mDrawable != null && mDrawable.getKey() == null
- && mDrawable.getPreviousKey() != null) {
+ && mDrawable.getPreviousKey() != null && mShouldUnbindOnDetachFromWindow) {
mDrawable.bind(mDrawable.getPreviousKey());
}
}
@@ -128,7 +144,8 @@ public class BitmapDrawableImageView extends ImageView {
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mAttachedToWindow = false;
- if (HAS_TRANSIENT_STATE_SUPPORTED && !hasTransientState()) {
+ if (HAS_TRANSIENT_STATE_SUPPORTED && !hasTransientState()
+ && mShouldUnbindOnDetachFromWindow) {
unbindDrawable(TEMPORARY);
}
}
@@ -136,7 +153,7 @@ public class BitmapDrawableImageView extends ImageView {
@Override
public void setHasTransientState(boolean hasTransientState) {
super.setHasTransientState(hasTransientState);
- if (!hasTransientState && !mAttachedToWindow) {
+ if (!hasTransientState && !mAttachedToWindow && mShouldUnbindOnDetachFromWindow) {
unbindDrawable(TEMPORARY);
}
}