Created
December 8, 2014 00:08
-
-
Save gouravd/5c5a3b6deafcdf444349 to your computer and use it in GitHub Desktop.
PicassoScaleToFitHeightWidthTransform
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 class ScaleToFitWidthHeightTransform implements com.squareup.picasso.Transformation { | |
private int mSize; // This has to be the maxHeight of the ImageView | |
private boolean isHeightScale; | |
public ScaleToFitWidthHeightTransform(int size, boolean isHeightScale){ | |
mSize =size; | |
this.isHeightScale = isHeightScale; | |
} | |
@Override | |
public Bitmap transform(Bitmap source) { | |
float scale; | |
int newSize; | |
Bitmap scaleBitmap; | |
if(isHeightScale){ | |
scale = (float) mSize / source.getHeight(); | |
newSize = Math.round(source.getWidth() * scale); | |
scaleBitmap= Bitmap.createScaledBitmap(source, newSize, mSize, true); | |
}else{ | |
scale = (float) mSize / source.getWidth(); | |
newSize = Math.round(source.getHeight() * scale); | |
scaleBitmap = Bitmap.createScaledBitmap(source, mSize,newSize, true); | |
} | |
if (scaleBitmap != source) { | |
source.recycle(); | |
} | |
return scaleBitmap; | |
} | |
@Override | |
public String key() { | |
return new StringBuilder("scaleRespectRatio") | |
.append(mSize) | |
.append(isHeightScale) | |
.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment