要将图像从指定的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,请替换yourUrl为您的Uri(content://media/external/images/1)。对于可绘制对象,请替换yourUrl为您的资源ID(R.drawable.image)。
在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,请在修改以下内容之前确保clear()所有ImageViewGlide可能正在管理ImageView:
@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); // 现在,视图已清除,您可以安全地设置自己的资源 viewHolder.imageView.setImageResource(R.drawable.missing_image); } else { Glide.with(context) .load(currentUrl) .into(myViewHolder.imageView); } }