Basic Usage

Loading images with Glide is easy and in many cases requires only a single line:

Glide.with(fragment)
    .load(myUrl)
    .into(imageView);

Cancelling loads you no longer need is simple too:

Glide.with(fragment).clear(imageView);

Although it’s good practice to clear loads you no longer need, you’re not required to do so. In fact, Glide will automatically clear the load and recycle any resources used by the load when the Activity or Fragment you pass in to Glide.with() is destroyed.

Customizing requests.

Glide offers a variety of options that can be applied to individual requests, including transformations, transitions, caching options etc.

Default options can be applied directly on a request:

Glide.with(fragment)
  .load(myUrl)
  .placeholder(placeholder)
  .fitCenter()
  .into(imageView);

Options can be shared across requests using the RequestOptions class:

RequestOptions sharedOptions = 
    new RequestOptions()
      .placeholder(placeholder)
      .fitCenter();

Glide.with(fragment)
  .load(myUrl)
  .apply(sharedOptions)
  .into(imageView1);

Glide.with(fragment)
  .load(myUrl)
  .apply(sharedOptions)
  .into(imageView2);

Glide’s API can be further extended to include custom options using Glide’s generated API for advanced use cases.

ListView and RecyclerView

Loading images in a ListView or RecyclerView uses the same load line as if you were loading in to a single View. Glide handles View re-use and request cancellation automatically:

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    String url = urls.get(position);
    Glide.with(fragment)
        .load(url)
        .into(holder.imageView);
}

You don’t need to null check your urls either, Glide will either clear out the view or set whatever placeholder Drawable or fallback Drawable you’ve specified if the url is null.

Glide’s sole requirement is that any re-usable View or Target that you may have started a load into at a previous position either has a new loaded started into it or is explicitly cleared via the clear() API.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    if (isImagePosition(position)) {
        String url = urls.get(position);
        Glide.with(fragment)
            .load(url)
            .into(holder.imageView);
    } else {
        Glide.with(fragment).clear(holder.imageView);
        holder.imageView.setImageDrawable(specialDrawable);
    }
}

By calling clear() or into(View) on the View, you’re cancelling the load and guaranteeing that Glide will not change the contents of the view after the call completes. If you forget to call clear() and don’t start a new load, the load you started into the same View for a previous position may complete after you set your special Drawable and change the contents of the View to an old image.

Although the examples we’ve shown here are for RecyclerView, the same principles apply to ListView as well.

Non-View Targets

In addition to loading Bitmaps and Drawables into Views, you can also start asynchronous loads into your own custom Targets:

Glide.with(context)
  .load(url)
  .into(new CustomTarget<Drawable>() {
    @Override
    public void onResourceReady(Drawable resource, Transition<Drawable> transition) {
      // Do something with the Drawable here.
    }

    @Override
    public void onLoadCleared(@Nullable Drawable placeholder) {
      // Remove the Drawable provided in onResourceReady from any Views and ensure 
      // no references to it remain.
    }
  });

There are a few gotchas with using custom Targets, so be sure to check out the Targets docs page for details.

Background Threads

Loading images on background threads is also straight forward using submit(int, int):

FutureTarget<Bitmap> futureTarget =
  Glide.with(context)
    .asBitmap()
    .load(url)
    .submit(width, height);

Bitmap bitmap = futureTarget.get();

// Do something with the Bitmap and then when you're done with it:
Glide.with(context).clear(futureTarget);

You can also start asynchronous loads on background threads the same way you would on a foreground thread if you don’t need the Bitmap or Drawable on the background thread itself:

Glide.with(context)
  .asBitmap()
  .load(url)
  .into(new Target<Bitmap>() {
    ...
  });