0

In a recyclerView where you need to load images from an array of drawables, which is better for smoother scrolling?

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    holder.iconImageView.setImageDrawable(drawables[position]);
}

or

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    Glide.with(mContext).load(drawables[position]).into(holder.iconImageView);
}

I don't understand how or what Glide is, from what I've read it's this magical thing that makes scrolling smoother. Is it true in this case? And is the Glide code I'm using above sufficient?

1 Answers1

2

You should use Glide in this scenario.

The first code executes on the main thread, inevitably slowing down processes. On the other hand, Glide loads in another thread and doesn't leave the UI hanging. So, Glide is preferable and you should notice significant improvement in loading time.

Also, Glide supports a cache mechanism which should help speed up image loading in most cases.

You can find here more information about Glide loading and caching on background threads.

Edit: Yes, you should use diskCacheStrategy and pass as a parameter DiskCacheStrategy.ALL or DiskCacheStrategy.SOURCE, to make sure Glide will use the bytes you cached using downloadOnly(). So, you can change your code like this:

Glide.with(mContext)
     .load(drawables[position])
     .diskCacheStrategy(DiskCacheStrategy.ALL)
     .into(holder.iconImageView);

Using Glide's cache mechanism is the fastest way to load images in your recyclerView and should result in a better and more smooth user experience.

Thanasis M
  • 1,144
  • 7
  • 22
  • Is it beneficial to use `diskCacheStrategy()` in my case? –  Jan 17 '21 at 10:48
  • What code should I add in my case to ensure the smoothest scrolling possible? –  Jan 17 '21 at 10:51
  • @HDNW Yes it is, I edited my answer to add some more info. – Thanasis M Jan 17 '21 at 11:51
  • What's `downloadOnly()`? Does it mean images downloaded from the internet? All the images I'm loading are already from the user's storage. –  Jan 17 '21 at 12:01
  • 1
    The downloadOnly() API is only used to make sure the bytes are available on disk. So, it is needed in order to download the bytes of an image into the disk cache so that it will be available to be retrieved later. In other words, it is used to store an image to Glide's cache. This image may have been downloaded from a Url in the internet or may be a Uri of a local file. So, it's independent to the way you fetch your images and I understand it's name may be misleading. – Thanasis M Jan 17 '21 at 12:48
  • To create a Uri of a local file and load it with Glide read this: https://stackoverflow.com/questions/34443171/load-image-from-sd-card-using-glide – Thanasis M Jan 17 '21 at 12:48
  • Not sure what I should exactly do. Should I just get rid of the Drawable array and instead use a Uri? Currently to get the images I first write `List packages = pm.queryIntentActivities(main, 0);` which is just a way to get info about all apps installed on the user's device, and the drawables are then gotten using `packages.get(i).loadIcon(pm);` where pm is a PackageManager. How do I go about optimizing this for Glide? –  Jan 17 '21 at 13:41
  • where `i` is the position inside the packages list –  Jan 17 '21 at 13:50
  • 1
    With Glide you can load images a variety of sources e.g. files, URIs, Android resources, bitmaps, drawables, etc. For each, the syntax stays the same. Just use the load() method to receive the image in one of these other formats. So, you don't need to use a uri. – Thanasis M Jan 17 '21 at 14:23
  • 1
    What you should do is try early on in your Activity/Fragment to use downloadOnly in order to store your drawable resource into glide's cache. Then when you want to show this image resource into a view you just use the code I posted wich uses diskCacheStrategy. – Thanasis M Jan 17 '21 at 14:30
  • So after I create the `drawables[]` array I then should do this: `Glide.with(context).load(drawables[position]).downloadOnly(2000, 2000);`, and then in onBindViewHolder I write `Glide.with(mContext).load(drawables[position]).diskCacheStrategy(DiskCacheStrategy.ALL).into(holder.iconImageView);`? –  Jan 17 '21 at 14:59
  • Yeah, that's the concept. – Thanasis M Jan 17 '21 at 15:02
  • Ok I did it that way and I'm getting the warning that "The result of downloadOnly is not used" –  Jan 17 '21 at 15:10
  • Are you sure this is the correct usage of Glide? –  Jan 17 '21 at 15:59
  • You are getting this warning because you ignore the return value of downloadOnly(). With the returned value you are given access to the underlying cache File, but you usually don’t want to interact with it. I'm positive this is the correct way to use Glide's cache mechanism since this snippet was in the official documentation. Also, for more details about Glide's cache, read this https://bumptech.github.io/glide/doc/caching.html#caching-in-glide . Here you will also find the appropriate way to use either disk or memory cache. – Thanasis M Jan 17 '21 at 17:08
  • I added `onlyRetrieveFromCache(true)` to it to verify if it was actually loading it from the cache. `Glide.with(mContext).load(drawables[position]).diskCacheStrategy(DiskCacheStrategy.ALL).onlyRetrieveFromCache(true).into(holder.iconImageView);` Non of the images were loaded. So it's not retrieving anything from cache, which means downloadOnly() isn't working. Incidentally this method is reported to be depreciated. What else is there to try? –  Jan 17 '21 at 18:42
  • Btw I placed the downloadAll() statement in the RecyclerView Constructor, the same place where I fill the drawables[] array. –  Jan 17 '21 at 19:07
  • Out of ideas? :) –  Jan 17 '21 at 22:52
  • @HDNW Here https://bumptech.github.io/glide/javadocs/400/com/bumptech/glide/RequestBuilder.html#downloadOnly-int-int- you can find the deprecated version of downloadOnly that you are using. You should instead use this https://bumptech.github.io/glide/javadocs/400/com/bumptech/glide/RequestManager.html#downloadOnly-- updated version of downloadOnly, according to the official documentation. – Thanasis M Jan 20 '21 at 18:04