Skip to content

Instantly share code, notes, and snippets.

@kyokomi
Last active August 26, 2016 02:14
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 kyokomi/602711fd1d34d8970bf615c85d6fad76 to your computer and use it in GitHub Desktop.
Save kyokomi/602711fd1d34d8970bf615c85d6fad76 to your computer and use it in GitHub Desktop.
import android.graphics.Bitmap;
import com.squareup.picasso.Transformation;
public class ScaleTransformation implements Transformation {
private final int baseSize;
/**
* @param baseSize px
*/
public ScaleTransformation(int baseSize) {
this.baseSize = baseSize;
}
@Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
if (size <= baseSize) {
return source;
}
float scale = (float)baseSize / (float)size;
int width = (int) (source.getWidth() * scale);
int height = (int) (source.getHeight() * scale);
if (width <= 0 || height <= 0) {
return source;
}
Bitmap result = Bitmap.createScaledBitmap(source, width, height, true);
if (result != source) {
source.recycle();
}
return result;
}
@Override
public String key() {
return "square" + baseSize;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment