Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Glide load image asynchronously?

I have a RecyclerView and each item of RecyclerView is having ImageView. I am loading the image in that ImageView using Glide. I saw some blog says Glide can load image asynchronously but I can not see this from the code below. It seems it only load one image when onBindViewHolder is invoked. How does it show asynchrony?

   public class GalleryAdapter extends RecyclerView.Adapter<GalleryAdapter.ViewHolder> {
        private Context mContext;
        private List<GalleryItem> mList;

        public GalleryAdapter(Context mContext, List<GalleryItem> mList) {
            this.mContext = mContext;
            this.mList = mList;
        }

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView mImageView;
            public ViewHolder(View itemView) {
                super(itemView);
                mImageView = (ImageView) itemView.findViewById(R.id.gallery_item);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_gallery,
                parent, false);
            ViewHolder vh = new ViewHolder(v);
            return vh;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            GalleryItem item = mList.get(position);
            holder.mImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            Glide.with(mContext)
                    .load(item.getUrl())
                    .thumbnail(0.5f)
                    .into(holder.mImageView);
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        public void addAll(List<GalleryItem> newList) {
            mList.addAll(newList);
        }

        public void clear() {
            mList.clear();
        }
    }
like image 862
dashenswen Avatar asked Jan 02 '17 04:01

dashenswen


People also ask

How does glide work?

How does the Glide caching mechanism work? By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.

Does glide run on main thread?

The into(ImageView) method of Glide requires you to call it only on main thread, but when you pass the loading to a Timer it will be executed in a background thread. What you can do is to retrieve a bitmap by calling get() instead of into() and then set that bitmap on the ImageView by calling setImageBitmap() .

How do you post images from URL on Glide?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.

How do you cache images on Glide?

How Glide Cache Works. By default, Glide checks multiple layers of caches before starting a new request for an image: Active resources — Is this image displayed in another View right now? Memory cache — Was this image recently loaded and still in memory?


1 Answers

According to glide documentation here and here you should use the into() method that returns FutureTarget and not the one that returns a Target. This method take width and height, not an ImageView : "If you actually want to interact with a decoded image on a background thread, instead of using downloadOnly you can use the version of into() that returns a FutureTarget."

like image 98
Laurent Avatar answered Oct 28 '22 21:10

Laurent