mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-15 07:28:30 +00:00
Move UriGlideRenderer to lib:image-editor.
This commit is contained in:
committed by
Greyson Parrelli
parent
7bd3482367
commit
7823d4f49f
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2026 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.util
|
||||
|
||||
import android.graphics.Bitmap
|
||||
import androidx.core.graphics.scale
|
||||
|
||||
/**
|
||||
* Creates a scaled bitmap with the given maximum dimensions while maintaining the original aspect ratio.
|
||||
*/
|
||||
fun Bitmap.scaleWithAspectRatio(maxWidth: Int, maxHeight: Int): Bitmap {
|
||||
if (getWidth() <= maxWidth && getHeight() <= maxHeight) {
|
||||
return this
|
||||
}
|
||||
|
||||
if (maxWidth <= 0 || maxHeight <= 0) {
|
||||
return this
|
||||
}
|
||||
|
||||
var newWidth = maxWidth
|
||||
var newHeight = maxHeight
|
||||
|
||||
val widthRatio: Float = getWidth() / maxWidth.toFloat()
|
||||
val heightRatio: Float = getHeight() / maxHeight.toFloat()
|
||||
|
||||
if (widthRatio > heightRatio) {
|
||||
newHeight = (getHeight() / widthRatio).toInt()
|
||||
} else {
|
||||
newWidth = (getWidth() / heightRatio).toInt()
|
||||
}
|
||||
|
||||
return scale(newWidth, newHeight)
|
||||
}
|
||||
Reference in New Issue
Block a user