Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@DHuckaby
Created June 1, 2012 18:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DHuckaby/cf945f200dfdb5a08c19 to your computer and use it in GitHub Desktop.
Save DHuckaby/cf945f200dfdb5a08c19 to your computer and use it in GitHub Desktop.
OptimizedImageView.java
public class OptimizedImageView extends ImageView {
private boolean mMeasuredExactly = false;
private boolean mBlockMeasurement = false;
public OptimizedImageView(Context context) {
super(context);
}
public OptimizedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OptimizedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setImageBitmap(Bitmap bm) {
mBlockMeasurement = true;
super.setImageBitmap(bm);
mBlockMeasurement = false;
}
@Override
public void requestLayout() {
if (mBlockMeasurement && mMeasuredExactly) {
// Ignore request
} else {
super.requestLayout();
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
mMeasuredExactly = isMeasuredExactly(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
private boolean isMeasuredExactly(int widthMeasureSpec, int heightMeasureSpec) {
int widthMeasureSpecMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMeasureSpecMode = MeasureSpec.getMode(heightMeasureSpec);
return widthMeasureSpecMode == MeasureSpec.EXACTLY && heightMeasureSpecMode == MeasureSpec.EXACTLY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment