From d6a5eee02ccdb8bddbefa39b0b47854333b59b7f Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Fri, 20 May 2022 09:51:23 +0200 Subject: [PATCH] preserve aspect ratio for pixel art --- .../habitrpg/android/habitica/ui/views/PixelArtView.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/views/PixelArtView.kt b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/views/PixelArtView.kt index 6311bc3a4..b486198c1 100644 --- a/Habitica/src/main/java/com/habitrpg/android/habitica/ui/views/PixelArtView.kt +++ b/Habitica/src/main/java/com/habitrpg/android/habitica/ui/views/PixelArtView.kt @@ -6,6 +6,7 @@ import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect import android.util.AttributeSet +import java.lang.Integer.min class PixelArtView @JvmOverloads constructor( context: Context, @@ -37,12 +38,13 @@ class PixelArtView @JvmOverloads constructor( private fun updateTargetRect() { var targetWidth = bitmap?.width ?: 0 var targetHeight = bitmap?.height ?: 0 + val smallestSide = min(width, height) - if (width > 0 && targetWidth > 0 && width != targetWidth) { - targetWidth = (targetWidth / 3) * (width / (targetWidth / 3)) + if (smallestSide > 0 && targetWidth > 0 && smallestSide != targetWidth) { + targetWidth = (targetWidth / 3) * (smallestSide / (targetWidth / 3)) } - if (height > 0 && targetHeight > 0 && height != targetHeight) { - targetHeight = (targetHeight / 3) * (height / (targetHeight / 3)) + if (smallestSide > 0 && targetHeight > 0 && smallestSide != targetHeight) { + targetHeight = (targetHeight / 3) * (smallestSide / (targetHeight / 3)) } val left = (width - targetWidth) / 2