-
-
Save Den-Rimus/0595a88a9424e005c7f2 to your computer and use it in GitHub Desktop.
bitmap resize
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
/** | |
* 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