Skip to content

Instantly share code, notes, and snippets.

@Den-Rimus
Created December 31, 2014 13:12
Show Gist options
  • Save Den-Rimus/0595a88a9424e005c7f2 to your computer and use it in GitHub Desktop.
Save Den-Rimus/0595a88a9424e005c7f2 to your computer and use it in GitHub Desktop.
bitmap resize
/**
* From here as-is: http://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/
*/
public static Bitmap resizeBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a Matrix for manipulations
Matrix matrix = new Matrix();
// Resize
matrix.postScale(scaleWidth, scaleHeight);
// Create new bitmap
return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
}
/**
* Same as {@link #resizeBitmap(android.graphics.Bitmap, int, int)} but takes one size parameter as<br>
* desired smallest side. Resizes keeping proportions.<br>
* <b>NOTE!</b> Assumes that your image is vertical!
*/
public static Bitmap resizeBitmap(Bitmap bm, int newWidth) {
float ratio = (float) bm.getHeight() / bm.getWidth();
return resizeBitmap(bm, Math.round(newWidth * ratio), newWidth);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment