Skip to content

Instantly share code, notes, and snippets.

@mortenjust
Created September 1, 2015 02:02
Show Gist options
  • Save mortenjust/11540046cd328c386094 to your computer and use it in GitHub Desktop.
Save mortenjust/11540046cd328c386094 to your computer and use it in GitHub Desktop.
resize bitmap android
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