Skip to content

Instantly share code, notes, and snippets.

@evant
Last active August 29, 2015 14:05
Show Gist options
  • Save evant/092770e7aa0f14fad2d3 to your computer and use it in GitHub Desktop.
Save evant/092770e7aa0f14fad2d3 to your computer and use it in GitHub Desktop.
Picasso Resize Transform
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