-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Share Playback Stories in 9:16 aspect ratio
- Loading branch information
Showing
3 changed files
with
86 additions
and
3 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
43 changes: 43 additions & 0 deletions
43
modules/services/utils/src/main/java/au/com/shiftyjelly/pocketcasts/utils/BitmapUtil.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,43 @@ | ||
package au.com.shiftyjelly.pocketcasts.utils | ||
|
||
import android.graphics.Bitmap | ||
import android.graphics.Color | ||
import android.graphics.Paint | ||
import androidx.annotation.ColorInt | ||
import androidx.core.graphics.applyCanvas | ||
import kotlin.math.abs | ||
import kotlin.math.roundToInt | ||
|
||
fun Bitmap.fitToAspectRatio( | ||
aspectRatio: Float, | ||
@ColorInt backgroundColor: Int = Color.BLACK, | ||
bitmapConfig: Bitmap.Config = Bitmap.Config.ARGB_8888, | ||
): Bitmap { | ||
val (newWidth, newHeight) = calculateNearestSize(width, height, aspectRatio) | ||
return Bitmap.createBitmap(newWidth, newHeight, bitmapConfig).applyCanvas { | ||
val paint = Paint().apply { | ||
isDither = true | ||
color = backgroundColor | ||
} | ||
drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint) | ||
drawBitmap( | ||
this@fitToAspectRatio, | ||
abs(width - this@fitToAspectRatio.width).toFloat() / 2, | ||
abs(height - this@fitToAspectRatio.height).toFloat() / 2, | ||
null, | ||
) | ||
} | ||
} | ||
|
||
internal fun calculateNearestSize( | ||
width: Int, | ||
height: Int, | ||
aspectRatio: Float, | ||
): Pair<Int, Int> { | ||
check(aspectRatio > 0) { "Aspect ratio must be a positive number: $aspectRatio" } | ||
return when (width.toFloat() / height) { | ||
aspectRatio -> width to height | ||
in 0f..aspectRatio -> (height * aspectRatio).roundToInt() to height | ||
else -> width to (width / aspectRatio).roundToInt() | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
modules/services/utils/src/test/java/au/com/shiftyjelly/pocketcasts/utils/BitmapUtilTest.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,38 @@ | ||
package au.com.shiftyjelly.pocketcasts.utils | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class BitmapUtilTest { | ||
@Test | ||
fun `fail for zero aspect ratio`() { | ||
try { | ||
calculateNearestSize(0, 0, aspectRatio = 0f) | ||
} catch (e: IllegalStateException) { | ||
assertEquals("Aspect ratio must be a positive number: 0.0", e.message) | ||
} | ||
} | ||
|
||
@Test | ||
fun `fail for negative aspect ratio`() { | ||
try { | ||
calculateNearestSize(0, 0, aspectRatio = -1f) | ||
} catch (e: IllegalStateException) { | ||
assertEquals("Aspect ratio must be a positive number: -1.0", e.message) | ||
} | ||
} | ||
|
||
@Test | ||
fun `calculate to fit horizontally`() { | ||
val size = calculateNearestSize(width = 100, height = 200, aspectRatio = 9f / 16) | ||
|
||
assertEquals(113 to 200, size) | ||
} | ||
|
||
@Test | ||
fun `calculate to fit vertically`() { | ||
val size = calculateNearestSize(width = 200, height = 100, aspectRatio = 9f / 16) | ||
|
||
assertEquals(200 to 356, size) | ||
} | ||
} |