[Android] Widgets: fix the cover ratio by center cropping the bitmap

Nicolas Pomepuy git at videolan.org
Wed May 18 09:57:58 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Wed May 11 13:14:49 2022 +0200| [c5fe2531d77615267509f6c62502416207246f85] | committer: Duncan McNamara

Widgets: fix the cover ratio by center cropping the bitmap

> https://code.videolan.org/videolan/vlc-android/commit/c5fe2531d77615267509f6c62502416207246f85
---

 .../src/org/videolan/vlc/gui/helpers/BitmapUtil.kt | 25 ++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
index 10d147764..da0ef7e59 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/BitmapUtil.kt
@@ -209,7 +209,7 @@ object BitmapUtil {
 
         val paint = Paint()
         paint.isAntiAlias = true
-        paint.color = -0xbdbdbe
+        paint.color = -0xFFFFFF
 
         val rect = Rect(0, 0, w, h)
         val rectF = RectF(rect)
@@ -218,7 +218,9 @@ object BitmapUtil {
         canvas.drawCircle(rectF.left + rectF.width() / 2, rectF.top + rectF.height() / 2, (radius / 2).toFloat(), paint)
 
         paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
-        canvas.drawBitmap(bm, rect, rect, paint)
+        //depending on the bm ratio, modify the bounds to keep a square bitmap
+        val bounds = bm.getMaximalSquareBounds()
+        canvas.drawBitmap(bm, bounds, rect, paint)
 
         return bmOut
     }
@@ -246,7 +248,7 @@ object BitmapUtil {
 
         val paint = Paint()
         paint.isAntiAlias = true
-        paint.color = -0xbdbdbe
+        paint.color = -0xFFFFFF
 
         val rect = Rect(0, 0, w, h)
         val rectF = RectF(rect)
@@ -260,11 +262,26 @@ object BitmapUtil {
         if (!bottomRight) canvas.drawRect(RectF(w.toFloat() - radius, h.toFloat() - radius, w.toFloat(),h.toFloat()), paint)
 
         paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
-        canvas.drawBitmap(bm, Rect(0, 0, bm.width, bm.height), rect, paint)
+
+        //depending on the bm ratio, modify the bounds to keep a square bitmap
+        val bounds = bm.getMaximalSquareBounds()
+
+        canvas.drawBitmap(bm, bounds, rect, paint)
 
         return bmOut
     }
 
+    /**
+     * Get the bound of the maximal size to cut a bitmap into a square
+     *
+     * @return the bounds
+     */
+    private fun Bitmap.getMaximalSquareBounds() = when {
+        width > height -> Rect((width - height) / 2, 0, height + ((width - height) / 2), height)
+        width < height -> Rect(0, (height - width) / 2, width, width + ((height - width) / 2))
+        else -> Rect(0, 0, width, height)
+    }
+
 }
 
 /**



More information about the Android mailing list