Skip to content

Instantly share code, notes, and snippets.

@marcelocra
Last active September 24, 2015 01:40
Show Gist options
  • Save marcelocra/846e6197f78b7ca86b2a to your computer and use it in GitHub Desktop.
Save marcelocra/846e6197f78b7ca86b2a to your computer and use it in GitHub Desktop.
Access ImageView info when using Picasso library
// This can be used to load images when you don't know their sizes beforehand.
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// Need to remove the listener, otherwise weird things will happen.
if (Build.VERSION.SDK_INT < 16) {
imageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
Picasso.with(mContext)
.load(mImageUrls.get(position))
// The dimension you leave with value 0 will be set based on the view size.
.resize(imageView.getWidth(), 0)
.into(imageView);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment