是否可以使用 Glide 下载图像以加载到 TextView 中?
Is it possible to use Glide to download image to load into a TextView?
在我的应用程序中,我从 URL 下载图像并通过 Glide 将其设置到 ImageView 中,但是,我试图删除一些不必要的布局,因此是否可以使用 Glide 下载图像并设置为 TextView?
try {
Glide.with(holder.logo.getContext())
.load(standingObjectItems.get(position).getImgId()).diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.mipmap.ic_launcher)
.placeholder(R.mipmap.ic_launcher)
.into(holder.logo);
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
e.printStackTrace();
}
Glide.with(left.getContext())
.load(((FixturesListObject) object).getHomeIcon())
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
left.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(left.getResources(),resource), null, null);
}
});
使用 Glide 4.7.1:
Glide.with(context)
.load(someUrl)
/* Because we can*/
.apply(RequestOptions.circleCropTransform())
/* If a fallback is not set, null models will cause the error drawable to be displayed. If
* the error drawable is not set, the placeholder will be displayed.*/
.apply(RequestOptions.placeholderOf(R.drawable.default_photo))
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource,
@Nullable Transition<? super Drawable> transition) {
/* Set a drawable to the left of textView */
textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null);
}
});
在 Glide 4.9.0 中,SimpleTarget 已弃用。您可以改用 CustomTarget。
Glide.with(myFragmentOrActivity)
.load(imageUrl)
.into(new CustomTarget<Drawable>(100,100) {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition)
{
left.setCompoundDrawablesWithIntrinsicBounds(null, resource, null, null);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder)
{
left.setCompoundDrawablesWithIntrinsicBounds(null, placeholder, null, null);
}
});
这里有一个简单的例子,说明如何使用 Kotlin。
GlideApp.with(context)
.load("url")
.placeholder(R.drawable.your_placeholder)
.error(R.drawable.your_error_image)
.into(object : CustomTarget<Drawable>(100, 100) {
override fun onLoadCleared(drawable: Drawable?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
}
override fun onResourceReady(res: Drawable, transition: com.bumptech.glide.request.transition.Transition<in Drawable>?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(res,null,null,null)
}
})
在我的应用程序中,我从 URL 下载图像并通过 Glide 将其设置到 ImageView 中,但是,我试图删除一些不必要的布局,因此是否可以使用 Glide 下载图像并设置为 TextView?
try {
Glide.with(holder.logo.getContext())
.load(standingObjectItems.get(position).getImgId()).diskCacheStrategy(DiskCacheStrategy.ALL)
.error(R.mipmap.ic_launcher)
.placeholder(R.mipmap.ic_launcher)
.into(holder.logo);
} catch (IllegalArgumentException | IndexOutOfBoundsException e) {
e.printStackTrace();
}
Glide.with(left.getContext())
.load(((FixturesListObject) object).getHomeIcon())
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
left.setCompoundDrawablesWithIntrinsicBounds(null, new BitmapDrawable(left.getResources(),resource), null, null);
}
});
使用 Glide 4.7.1:
Glide.with(context)
.load(someUrl)
/* Because we can*/
.apply(RequestOptions.circleCropTransform())
/* If a fallback is not set, null models will cause the error drawable to be displayed. If
* the error drawable is not set, the placeholder will be displayed.*/
.apply(RequestOptions.placeholderOf(R.drawable.default_photo))
.into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource,
@Nullable Transition<? super Drawable> transition) {
/* Set a drawable to the left of textView */
textView.setCompoundDrawablesWithIntrinsicBounds(resource, null, null, null);
}
});
在 Glide 4.9.0 中,SimpleTarget 已弃用。您可以改用 CustomTarget。
Glide.with(myFragmentOrActivity)
.load(imageUrl)
.into(new CustomTarget<Drawable>(100,100) {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition)
{
left.setCompoundDrawablesWithIntrinsicBounds(null, resource, null, null);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder)
{
left.setCompoundDrawablesWithIntrinsicBounds(null, placeholder, null, null);
}
});
这里有一个简单的例子,说明如何使用 Kotlin。
GlideApp.with(context)
.load("url")
.placeholder(R.drawable.your_placeholder)
.error(R.drawable.your_error_image)
.into(object : CustomTarget<Drawable>(100, 100) {
override fun onLoadCleared(drawable: Drawable?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null)
}
override fun onResourceReady(res: Drawable, transition: com.bumptech.glide.request.transition.Transition<in Drawable>?) {
header.companyLogoJob.setCompoundDrawablesWithIntrinsicBounds(res,null,null,null)
}
})