with Aspect Ratio
fun ImageBitmap.withAspectRatio(aspectRatio: Float, scale: Boolean = false, targetHeight: Int? = null): ImageBitmap
Adjusts this ImageBitmap to a target aspect ratio.
Example usage:
val image: ImageBitmap = // load image
// Crop to square
val square = image.withAspectRatio(1f)
// Crop to 16:9
val croppedWide = image.withAspectRatio(16f / 9f)
// Scale to 16:9 with height = 1080px
val scaledWide = image.withAspectRatio(16f / 9f, scale = true, targetHeight = 1080)Content copied to clipboard
Parameters
aspect Ratio
The desired width/height ratio (e.g. 16f / 9f, 1f for square).
scale
Whether to scale instead of crop. Default = false (crop).
target Height
Optional target height if scaling (only used when scale = true).