Created
June 23, 2015 01:57
-
-
Save lenamuit/9ae1458b43fc2cf28db2 to your computer and use it in GitHub Desktop.
Picasso - Load image wrap content
This file contains hidden or 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 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