Created
September 1, 2015 02:02
-
-
Save mortenjust/11540046cd328c386094 to your computer and use it in GitHub Desktop.
resize bitmap android
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 Bitmap resizeBitmap(Bitmap b, int newWidthDp){ | |
Float density = getResources().getDisplayMetrics().density; | |
int newWidth = newWidthDp * Math.round(density); | |
int width = b.getWidth(); | |
int height = b.getHeight(); | |
float scaleWidth = ((float) newWidth) / width; | |
float ratio = (float) width / newWidth; | |
int newHeight = (int) (height / ratio); | |
float scaleHeight = ((float) newHeight) / height; | |
Matrix matrix = new Matrix(); | |
matrix.postScale(scaleWidth, scaleHeight); | |
Bitmap resizedBitmap = Bitmap.createBitmap(b, 0, 0, | |
width, height, matrix, true); | |
return (resizedBitmap); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment