Created
October 9, 2013 13:19
-
-
Save PureSpider/6901156 to your computer and use it in GitHub Desktop.
Resize image to new width and height.
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
public Bitmap resizeBitmap(Bitmap in, float newWidth, float newHeight) { | |
// http://forrst.com/posts/Bitmap_Drawable_Resize_on_Android-mjr | |
Bitmap bitmapOrig = in; | |
DisplayMetrics metrics = new DisplayMetrics(); | |
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); | |
windowManager.getDefaultDisplay().getMetrics(metrics); | |
float logicalDensity = metrics.density; | |
float scaleWidth = ((float) newWidth) / bitmapOrig.getWidth(); | |
float scaleHeight = ((float) newHeight) / bitmapOrig.getHeight(); | |
Matrix matrix = new Matrix(); | |
matrix.postScale(scaleWidth * logicalDensity, scaleHeight * logicalDensity); | |
return Bitmap.createBitmap(bitmapOrig, 0, 0, bitmapOrig.getWidth(), bitmapOrig.getHeight(), matrix, true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment