[Android] Fix bitmap decoding for vector drawables

Nicolas Pomepuy git at videolan.org
Thu Apr 16 15:43:08 CEST 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Mar 19 07:36:20 2020 +0100| [58697c3c34288e68e2ca649904ed8018965ce5d9] | committer: Nicolas Pomepuy

Fix bitmap decoding for vector drawables

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

 .../org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt |  1 +
 .../src/org/videolan/vlc/gui/helpers/BitmapUtil.kt     | 18 ++++++++++++++++++
 .../src/org/videolan/vlc/media/MediaSessionBrowser.kt  |  1 +
 3 files changed, 20 insertions(+)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
index 9828bb20a..f94d85091 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserAdapter.kt
@@ -50,6 +50,7 @@ import org.videolan.vlc.R
 import org.videolan.vlc.databinding.BrowserItemBinding
 import org.videolan.vlc.databinding.BrowserItemSeparatorBinding
 import org.videolan.vlc.gui.DiffUtilAdapter
+import org.videolan.vlc.gui.helpers.BitmapUtil
 import org.videolan.vlc.gui.helpers.MarqueeViewHolder
 import org.videolan.vlc.gui.helpers.SelectorViewHolder
 import org.videolan.vlc.gui.helpers.enableMarqueeEffect
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 e52506374..8cb10bb10 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
@@ -115,6 +115,24 @@ object BitmapUtil {
         }
 
     }
+
+    fun getBitmapFromVectorDrawable(context: Context, @DrawableRes drawableId: Int): Bitmap? {
+        var drawable: Drawable = ContextCompat.getDrawable(context, drawableId) ?: return null
+        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
+            drawable = DrawableCompat.wrap(drawable).mutate()
+        }
+        return when (drawable) {
+            is BitmapDrawable -> drawable.bitmap
+            is VectorDrawableCompat, is VectorDrawable -> {
+                val bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
+                val canvas = Canvas(bitmap)
+                drawable.setBounds(0, 0, canvas.width, canvas.height)
+                drawable.draw(canvas)
+                bitmap
+            }
+            else -> BitmapFactory.decodeResource(context.resources, drawableId)
+        }
+    }
 }
 
 fun Context.getBitmapFromDrawable(@DrawableRes drawableId: Int): Bitmap? {
diff --git a/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt b/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
index 82eae1dce..1dc31cc5f 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
@@ -49,6 +49,7 @@ import org.videolan.vlc.extensions.ExtensionManagerService.ExtensionManagerActiv
 import org.videolan.vlc.extensions.ExtensionsManager
 import org.videolan.vlc.extensions.api.VLCExtensionItem
 import org.videolan.vlc.gui.helpers.AudioUtil.readCoverBitmap
+import org.videolan.vlc.gui.helpers.BitmapUtil
 import org.videolan.vlc.gui.helpers.UiTools.getDefaultAudioDrawable
 import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
 import org.videolan.vlc.media.MediaUtils.getMediaSubtitle



More information about the Android mailing list