-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#17 in progress - refactor in PhotoHelper
- Loading branch information
Showing
9 changed files
with
105 additions
and
64 deletions.
There are no files selected for viewing
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
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
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
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
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
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
45 changes: 45 additions & 0 deletions
45
app/src/main/java/pl/marianjureczko/poszukiwacz/shared/PhotoScalingHelper.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package pl.marianjureczko.poszukiwacz.shared | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Matrix | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
object PhotoScalingHelper { | ||
|
||
//TODO t: add suspend | ||
//TODO t: try to test with https://robolectric.org/androidx_test/ | ||
/** | ||
* Scales the photo, but keeps the aspect ratio | ||
* @param maxHeight - the maximum height after scaling | ||
* @param maxWidth - the maximum width after scaling | ||
*/ | ||
fun scalePhotoKeepRatio(photo: Bitmap, maxHeight: Float, maxWidth: Float): Bitmap { | ||
val widthFactor = maxWidth / photo.width | ||
val heightFactor = maxHeight / photo.height | ||
val factor = min(widthFactor, heightFactor) | ||
val matrix = Matrix() | ||
matrix.postScale(factor, factor) | ||
return Bitmap.createBitmap(photo, 0, 0, photo.width, photo.height, matrix, false) | ||
} | ||
|
||
/** | ||
* Keeps aspect ratio, but scales down so that the greater dimension goes down to 800 px | ||
* @param matrix for unit tests | ||
*/ | ||
fun createScalingMatrix(imageWidth: Int, imageHeight: Int, matrix: Matrix = Matrix()): Matrix { | ||
val scalingFactor = scalingFactorToGoDownTo800px(imageWidth, imageHeight) | ||
matrix.postScale(scalingFactor, scalingFactor) | ||
return matrix | ||
} | ||
|
||
private fun scalingFactorToGoDownTo800px(width: Int, height: Int): Float { | ||
val greater = max(width, height).toFloat() | ||
val wanted = 800F | ||
return if (greater < 800) { | ||
1F; | ||
} else { | ||
wanted / greater; | ||
} | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
app/src/test/java/pl/marianjureczko/poszukiwacz/shared/PhotoScalingHelperTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package pl.marianjureczko.poszukiwacz.shared | ||
|
||
import android.graphics.Matrix | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.BDDMockito.then | ||
import org.mockito.Mock | ||
import org.mockito.Mockito.times | ||
import org.mockito.junit.jupiter.MockitoExtension | ||
|
||
@ExtendWith(MockitoExtension::class) | ||
class PhotoScalingHelperTest { | ||
|
||
@Mock | ||
private lateinit var matrix: Matrix | ||
|
||
@Test | ||
fun `SHOULD scale width to ~ 800px WHEN width is greater than height`() { | ||
//when | ||
val actual = PhotoScalingHelper.createScalingMatrix(950, 1000, matrix) | ||
|
||
//then | ||
assertThat(actual).isSameAs(matrix) | ||
val expected = 0.8f | ||
then(matrix).should(times(1)).postScale(expected, expected) | ||
} | ||
|
||
@Test | ||
fun `SHOULD scale height to ~ 800px WHEN height is greater than width`() { | ||
//when | ||
val actual = PhotoScalingHelper.createScalingMatrix(1000, 900, matrix) | ||
|
||
//then | ||
assertThat(actual).isSameAs(matrix) | ||
val expected = 0.8f | ||
then(matrix).should(times(1)).postScale(expected, expected) | ||
} | ||
|
||
@Test | ||
fun `SHOULD scale to ~ 800px WHEN image is large`() { | ||
//when | ||
val actual = PhotoScalingHelper.createScalingMatrix(4000, 4000, matrix) | ||
|
||
//then | ||
assertThat(actual).isSameAs(matrix) | ||
val expected = 0.2f | ||
then(matrix).should(times(1)).postScale(expected, expected) | ||
} | ||
} |