Skip to content

Instantly share code, notes, and snippets.

@px-amaac
Created August 26, 2014 18:47
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 px-amaac/89c9aba413ea39fc6eef to your computer and use it in GitHub Desktop.
Save px-amaac/89c9aba413ea39fc6eef to your computer and use it in GitHub Desktop.
public void bindTo(FSStore item, Picasso picasso) {
targetImage = item.getImage();
picasso.setLoggingEnabled(true);
imageRequest = picasso.load(targetImage.getUrl());
distance.setText(String.valueOf(item.getDistance()) + "miles away");
imageAspectRatio = 1f * targetImage.getWidth() / targetImage.getHeight();
requestLayout();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mode = MeasureSpec.getMode(widthMeasureSpec);
if (mode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("layout_width must be match_parent");
}
int columnWidth = MeasureSpec.getSize(widthMeasureSpec);
if(mContext != null) {
distanceHeight = Utils.getTextHeight(mContext, distance.getText() + "miles away", distance.getTextSize(), columnWidth);
}
// Honor aspect ratio for height but no larger than 2x width.
int imageHeight = Math.min((int) (columnWidth / imageAspectRatio), columnWidth * 2);
int imageHeightMeasureSpec = MeasureSpec.makeMeasureSpec(imageHeight, MeasureSpec.EXACTLY);
int textHeightMeasureSpec = MeasureSpec.makeMeasureSpec(distanceHeight, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, imageHeightMeasureSpec + textHeightMeasureSpec);
if (imageRequest != null) {
imageRequest.resize(columnWidth, imageHeight).into(image);
imageRequest = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment