Created
March 21, 2013 17:18
-
-
Save chrisjenx/5214815 to your computer and use it in GitHub Desktop.
SmartImageView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created with Intellij with Android, BIZZBY product. | |
* See licencing for usage of this code. | |
* <p/> | |
* User: chris | |
* Date: 18/02/2013 | |
* Time: 10:43 | |
*/ | |
public class UriImageView extends ImageView | |
{ | |
/** | |
* Set this after the measure pass to what ever you want, if true the ImageView will not requestLayout | |
* after setting an image, nice optimization for fixed image sizes | |
*/ | |
protected boolean mIsFixedSize = false; | |
private boolean mBlockLayout = false; | |
public UriImageView(final Context context) | |
{ | |
this(context, null); | |
} | |
public UriImageView(final Context context, final AttributeSet attrs) | |
{ | |
super(context, attrs); | |
} | |
public UriImageView(final Context context, final AttributeSet attrs, final int defStyle) | |
{ | |
this(context, attrs); | |
} | |
@Override | |
public void requestLayout() | |
{ | |
if (!mBlockLayout) | |
super.requestLayout(); | |
} | |
@Override | |
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) | |
{ | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
// If we define the size then | |
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY && MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) | |
mIsFixedSize = true; | |
} | |
@Override | |
public void setImageResource(final int resId) | |
{ | |
blockLayoutIfPossible(); | |
super.setImageResource(resId); | |
mBlockLayout = false; | |
} | |
@Override | |
public void setImageURI(final Uri uri) | |
{ | |
blockLayoutIfPossible(); | |
super.setImageURI(uri); | |
mBlockLayout = false; | |
} | |
/** | |
* Set Image Drawable is called by setImageBitmap so you don't need to override setImageBitmap | |
* | |
* @param drawable | |
*/ | |
@Override | |
public void setImageDrawable(final Drawable drawable) | |
{ | |
blockLayoutIfPossible(); | |
super.setImageDrawable(drawable); | |
mBlockLayout = false; | |
} | |
protected final void blockLayoutIfPossible() | |
{ | |
if (mBlockLayout) | |
{ | |
mBlockLayout = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment