-
-
Save DHuckaby/cf945f200dfdb5a08c19 to your computer and use it in GitHub Desktop.
OptimizedImageView.java
This file contains 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
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