BitmapDrawableImageView.java revision 9024423249c9b1779f523cc4fe757fda2d205844
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.bitmap.view; 18 19import android.content.Context; 20import android.graphics.Bitmap; 21import android.graphics.drawable.Drawable; 22import android.net.Uri; 23import android.util.AttributeSet; 24import android.widget.ImageView; 25 26import com.android.bitmap.drawable.BasicBitmapDrawable; 27 28/** 29 * A helpful ImageView replacement that can generally be used in lieu of ImageView. 30 * BitmapDrawableImageView has logic to unbind its BasicBitmapDrawable when it is detached from the 31 * window. 32 */ 33public class BitmapDrawableImageView extends ImageView { 34 private BasicBitmapDrawable mDrawable; 35 private boolean mAttachedToWindow; 36 37 public BitmapDrawableImageView(final Context context) { 38 this(context, null); 39 } 40 41 public BitmapDrawableImageView(final Context context, final AttributeSet attrs) { 42 this(context, attrs, 0); 43 } 44 45 public BitmapDrawableImageView(final Context context, final AttributeSet attrs, 46 final int defStyle) { 47 super(context, attrs, defStyle); 48 } 49 50 /** 51 * Get the source drawable for this BitmapDrawableImageView. 52 * @return The source drawable casted to the given type, or null if the type does not match. 53 */ 54 @SuppressWarnings("unchecked") // Cast to type parameter. 55 public <E extends BasicBitmapDrawable> E getTypedDrawable() { 56 try { 57 return (E) mDrawable; 58 } catch (Exception ignored) { 59 return null; 60 } 61 } 62 63 /** 64 * Set the given drawable as the source for this BitmapDrawableImageView. 65 * @param drawable The source drawable. 66 */ 67 public <E extends BasicBitmapDrawable> void setTypedDrawable(E drawable) { 68 super.setImageDrawable(drawable); 69 unbindDrawable(); 70 mDrawable = drawable; 71 } 72 73 private void unbindDrawable() { 74 if (mDrawable != null) { 75 mDrawable.unbind(); 76 } 77 } 78 79 @Override 80 public void setImageResource(final int resId) { 81 super.setImageResource(resId); 82 unbindDrawable(); 83 mDrawable = null; 84 } 85 86 @Override 87 public void setImageURI(final Uri uri) { 88 super.setImageURI(uri); 89 unbindDrawable(); 90 mDrawable = null; 91 } 92 93 @Override 94 public void setImageDrawable(final Drawable drawable) { 95 super.setImageDrawable(drawable); 96 unbindDrawable(); 97 mDrawable = null; 98 } 99 100 @Override 101 public void setImageBitmap(final Bitmap bm) { 102 super.setImageBitmap(bm); 103 unbindDrawable(); 104 mDrawable = null; 105 } 106 107 @Override 108 protected void onAttachedToWindow() { 109 super.onAttachedToWindow(); 110 mAttachedToWindow = true; 111 } 112 113 @Override 114 protected void onDetachedFromWindow() { 115 super.onDetachedFromWindow(); 116 mAttachedToWindow = false; 117 if (!hasTransientState()) { 118 unbindDrawable(); 119 } 120 } 121 122 @Override 123 public void setHasTransientState(boolean hasTransientState) { 124 super.setHasTransientState(hasTransientState); 125 if (!hasTransientState && !mAttachedToWindow) { 126 unbindDrawable(); 127 } 128 } 129} 130