Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Last active February 24, 2016 01:42
Show Gist options
  • Save luanvuhlu/c1a5d8b6dd22460a98ef to your computer and use it in GitHub Desktop.
Save luanvuhlu/c1a5d8b6dd22460a98ef to your computer and use it in GitHub Desktop.
Scale bitmap image Android
public Bitmap scaleBitmapDown(Bitmap bitmap, int maxDimension) {
int originalWidth = bitmap.getWidth();
int originalHeight = bitmap.getHeight();
int resizedWidth = maxDimension;
int resizedHeight = maxDimension;
if (originalHeight > originalWidth) {
resizedHeight = maxDimension;
resizedWidth = (int) (resizedHeight * (float) originalWidth / (float) originalHeight);
} else if (originalWidth > originalHeight) {
resizedWidth = maxDimension;
resizedHeight = (int) (resizedWidth * (float) originalHeight / (float) originalWidth);
} else if (originalHeight == originalWidth) {
resizedHeight = maxDimension;
resizedWidth = maxDimension;
}
return Bitmap.createScaledBitmap(bitmap, resizedWidth, resizedHeight, false);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment