[Android] Avoid using bitmaps instead of resources when we can

Nicolas Pomepuy git at videolan.org
Thu Mar 19 11:55:23 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Mar 19 11:04:43 2020 +0100| [e83e8a3d9f59fa7b09834ee279e6b136dd7da0cc] | committer: Nicolas Pomepuy

Avoid using bitmaps instead of resources when we can

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

 .../src/org/videolan/vlc/gui/helpers/ImageLoader.kt  | 20 +++++++++++++++++---
 .../org/videolan/vlc/gui/view/CoverMediaSwitcher.kt  |  7 +------
 .../org/videolan/vlc/media/MediaSessionBrowser.kt    |  3 ++-
 3 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
index 70e56795a..64fb04268 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/helpers/ImageLoader.kt
@@ -226,9 +226,8 @@ private suspend fun getImage(v: View, item: MediaLibraryItem, binding: ViewDataB
     }
     val image = if (!bindChanged) obtainBitmap(item, width) else null
     if (image == null && tv) {
-        val imageTV = v.context.getBitmapFromDrawable(getTvIconRes(item))
         // binding is set to null to be sure to set the src and not the cover (background)
-        if (!bindChanged) updateImageView(bitmap = imageTV, target = v, vdb = null, updateScaleType = false, tv = tv, card = card)
+        if (!bindChanged) updateImageViewTv(getTvIconRes(item), v)
         binding?.removeOnRebindCallback(rebindCallbacks!!)
         return
     }
@@ -268,7 +267,22 @@ private suspend fun getPlaylistImage(v: View, item: MediaLibraryItem, binding: V
 }
 
 @MainThread
-fun updateImageView(bitmap: Bitmap?, target: View, vdb: ViewDataBinding?, updateScaleType: Boolean = true, tv : Boolean = false, card: Boolean = false) {
+fun updateImageViewTv(@DrawableRes res: Int, target: View) {
+    when (target) {
+        is ImageView -> {
+            target.scaleType = ImageView.ScaleType.CENTER_CROP
+            target.setImageResource(res)
+            target.visibility = View.VISIBLE
+        }
+        is ImageCardView -> {
+            target.mainImageView.scaleType = ImageView.ScaleType.CENTER_CROP
+            target.mainImageView.setImageResource(res)
+        }
+    }
+}
+
+ at MainThread
+fun updateImageView(bitmap: Bitmap?, target: View, vdb: ViewDataBinding?, updateScaleType: Boolean = true, tv: Boolean = false, card: Boolean = false) {
     if (bitmap === null || bitmap.width <= 1 || bitmap.height <= 1) return
     if (vdb !== null && !tv) {
         vdb.setVariable(BR.scaleType, if (card) ImageView.ScaleType.CENTER_CROP else ImageView.ScaleType.FIT_CENTER)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/view/CoverMediaSwitcher.kt b/application/vlc-android/src/org/videolan/vlc/gui/view/CoverMediaSwitcher.kt
index a8ef5ba9c..3f9fc8cce 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/view/CoverMediaSwitcher.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/view/CoverMediaSwitcher.kt
@@ -28,20 +28,15 @@ import android.widget.ImageView
 import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.ObsoleteCoroutinesApi
 import org.videolan.vlc.R
-import org.videolan.vlc.gui.helpers.getBitmapFromDrawable
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
 class CoverMediaSwitcher(context: Context, attrs: AttributeSet) : AudioMediaSwitcher(context, attrs) {
 
     override fun addMediaView(inflater: LayoutInflater, title: String?, artist: String?, cover: Bitmap?) {
-        var cover = cover
-
-        if (cover == null) cover = context.getBitmapFromDrawable(R.drawable.icon)
-
         val imageView = ImageView(context)
         imageView.scaleType = ImageView.ScaleType.FIT_CENTER
-        imageView.setImageBitmap(cover)
+        if (cover == null) imageView.setImageResource(R.drawable.icon) else imageView.setImageBitmap(cover)
         addView(imageView)
     }
 }
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 4166ed6ca..82eae1dce 100644
--- a/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
+++ b/application/vlc-android/src/org/videolan/vlc/media/MediaSessionBrowser.kt
@@ -30,6 +30,7 @@ import android.content.ServiceConnection
 import android.content.pm.PackageManager
 import android.content.res.Resources
 import android.graphics.Bitmap
+import android.graphics.BitmapFactory
 import android.graphics.drawable.BitmapDrawable
 import android.net.Uri
 import android.os.IBinder
@@ -164,7 +165,7 @@ class MediaSessionBrowser : ExtensionManagerActivity {
                                     try {
                                         extensionRes = context.packageManager
                                                 .getResourcesForApplication(extension.componentName().packageName)
-                                        b = context.getBitmapFromDrawable(iconRes)
+                                        b = BitmapFactory.decodeResource(extensionRes, iconRes)
                                     } catch (ignored: PackageManager.NameNotFoundException) {
                                     }
                                 }



More information about the Android mailing list