Created
December 10, 2015 21:54
-
-
Save akuafif/7b591ef4ac5b98ed7031 to your computer and use it in GitHub Desktop.
[Java/Android] Scale Bitmap/Image and Keep Aspect Ratio
This file contains 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
//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