Skip to content

Instantly share code, notes, and snippets.

@tomoima525
Created February 5, 2020 08:45
Show Gist options
  • Save tomoima525/150b5accca4d6fb42b5a554e68c7c8d2 to your computer and use it in GitHub Desktop.
Save tomoima525/150b5accca4d6fb42b5a554e68c7c8d2 to your computer and use it in GitHub Desktop.
resize bitmap
suspend fun getResizedBitmap(bm: Bitmap, newWidth: Int, newHeight: Int): Bitmap {
return withContext(Dispatchers.IO) {
val width = bm.width
val height = bm.height
val scaleWidth = newWidth.toFloat() / width
val scaleHeight = newHeight.toFloat() / height
val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)
val resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false)
bm.recycle()
resizedBitmap
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment