Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Created March 21, 2013 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjenx/5214815 to your computer and use it in GitHub Desktop.
Save chrisjenx/5214815 to your computer and use it in GitHub Desktop.
SmartImageView
/**
* 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