Skip to content

Instantly share code, notes, and snippets.

@lenamuit
Created June 23, 2015 01:57
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 lenamuit/9ae1458b43fc2cf28db2 to your computer and use it in GitHub Desktop.
Save lenamuit/9ae1458b43fc2cf28db2 to your computer and use it in GitHub Desktop.
Picasso - Load image wrap content
public static void loadImageWrapContent(Context context, final ImageView imageView, String url){
Picasso.with(context)
.load(url)
.placeholder(R.drawable.progress_indeterminate_horizontal_holo)
.transform(new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = imageView.getWidth();
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
if (targetHeight <=0 || targetWidth <=0) return source;
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
// Same bitmap is returned if sizes are the same
source.recycle();
}
return result;
}
@Override
public String key() {
return "key";
}
})
.into(imageView);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment