載入圖片

ImageView

要將指定 URL,Uri,資源 ID 或任何其他模型中的影象載入到 ImageView

ImageView imageView = (ImageView) findViewById(R.id.imageView);
String yourUrl = "http://www.yoururl.com/image.png";

Glide.with(context)
    .load(yourUrl)
    .into(imageView);

對於 Uris,用你的 Uri(content://media/external/images/1)替換 yourUrl。對於 Drawables,將 yourUrl 替換為你的資源 ID(R.drawable.image)。

RecyclerView 和 ListView

在 ListView 或 RecyclerView 中,你可以使用完全相同的行:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
    String currentUrl = myUrls.get(position); 

    Glide.with(context)
        .load(currentUrl)
        .into(myViewHolder.imageView);
}

如果你不想在 onBindViewHolder 中開始載入,請確保在修改 ImageView 之前你可以管理任何 ImageView Glide:

@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
    MyViewHolder myViewHolder = (MyViewHolder) viewHolder;
    String currentUrl = myUrls.get(position); 

    if (TextUtils.isEmpty(currentUrl)) {
        Glide.clear(viewHolder.imageView);
        // Now that the view has been cleared, you can safely set your own resource
        viewHolder.imageView.setImageResource(R.drawable.missing_image);
    } else {
        Glide.with(context)
            .load(currentUrl)
            .into(myViewHolder.imageView);
    }
}