Last active
August 26, 2016 02:14
-
-
Save kyokomi/602711fd1d34d8970bf615c85d6fad76 to your computer and use it in GitHub Desktop.
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
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