Skip to content

Instantly share code, notes, and snippets.

@zekierciyas
Last active August 4, 2022 19:25
Show Gist options
  • Save zekierciyas/79211b9959c8a1fc54bd3216b2e724e1 to your computer and use it in GitHub Desktop.
Save zekierciyas/79211b9959c8a1fc54bd3216b2e724e1 to your computer and use it in GitHub Desktop.
BitmapExtentions
fun Bitmap.resizeImage(image: Bitmap, maxWidth: Int, maxHeight: Int, onComplete: (bitmap: Bitmap?) -> Unit) {
val width = image.width
val height = image.height
val scaleWidth = maxWidth.toFloat() / width
val scaleHeight = maxHeight.toFloat() / height
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
// Create a New bitmap
val resizedBitmap = Bitmap.createBitmap(
image, 0, 0, width, height, matrix, false)
onComplete(resizedBitmap)
resizedBitmap.recycle()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment