Skip to content

Instantly share code, notes, and snippets.

@akuafif
Created December 10, 2015 21:54
Show Gist options
  • Save akuafif/7b591ef4ac5b98ed7031 to your computer and use it in GitHub Desktop.
Save akuafif/7b591ef4ac5b98ed7031 to your computer and use it in GitHub Desktop.
[Java/Android] Scale Bitmap/Image and Keep Aspect Ratio
//rescale image
public static Bitmap scaleBitmapAndKeepRation(Bitmap TargetBmp,int reqHeightInPixels,int reqWidthInPixels)
{
if(TargetBmp.getWidth() >= reqHeightInPixels && TargetBmp.getHeight() >= reqHeightInPixels) {
Matrix m = new Matrix();
m.setRectToRect(new RectF(0, 0, TargetBmp.getWidth(), TargetBmp.getHeight()), new RectF(0, 0, reqWidthInPixels, reqHeightInPixels), Matrix.ScaleToFit.CENTER);
Bitmap scaledBitmap = Bitmap.createBitmap(TargetBmp, 0, 0, TargetBmp.getWidth(), TargetBmp.getHeight(), m, true);
return scaledBitmap;
}
else
{
return TargetBmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment