Last active
August 21, 2017 17:36
-
-
Save InsanusMokrassar/9138fc3b8d73a2ad31f91bf287155d1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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