Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save InsanusMokrassar/9138fc3b8d73a2ad31f91bf287155d1b to your computer and use it in GitHub Desktop.
Save InsanusMokrassar/9138fc3b8d73a2ad31f91bf287155d1b to your computer and use it in GitHub Desktop.
fun calculateMeasure(
widthMeasureSpec: Int,
heightMeasureSpec: Int,
heightRatio: Int,
widthRatio: Int
): Pair<Int, Int> {
val originalWidth = MeasureSpec.getSize(widthMeasureSpec)
val originalHeight = MeasureSpec.getSize(heightMeasureSpec)
val calculatedHeight = originalWidth * heightRatio / widthRatio
val finalWidth: Int
val finalHeight: Int
if (calculatedHeight > originalHeight) {
finalWidth = originalHeight * widthRatio / heightRatio
finalHeight = originalHeight
} else {
finalWidth = originalWidth
finalHeight = calculatedHeight
}
return Pair(
MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment