Last active
August 29, 2015 14:05
-
-
Save evant/092770e7aa0f14fad2d3 to your computer and use it in GitHub Desktop.
Picasso Resize Transform
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 ResizeTransformation extends Transformation { | |
@Override public | |
Bitmap transform(Bitmap source) { | |
int targetWidth = getScreenWidth(); // Implementation left as an exercise for the reader. | |
if (targetWidth < source.getWidth()) { | |
double aspectRatio = (double) source.getHeight() / (double) source.getWidth(); | |
int targetHeight = (int) (targetWidth * aspectRatio); | |
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false); | |
if (result != source) { | |
// Same bitmap is returned if sizes are the same | |
source.recycle(); | |
} | |
return result; | |
} else { | |
return source; | |
} | |
} | |
@Override | |
public String key() { return "resize_transform"; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment