确保您使用的是Lastest version
implementation 'com.github.bumptech.glide:glide:4.10.0'
科特林:
Glide.with(this)
.asBitmap()
.load(imagePath)
.into(object : CustomTarget<Bitmap>(){
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
imageView.setImageBitmap(resource)
}
override fun onLoadCleared(placeholder: Drawable?) {
// this is called when imageView is cleared on lifecycle call or for
// some other reason.
// if you are referencing the bitmap somewhere else too other than this imageView
// clear it here as you can no longer have the bitmap
}
})
位图大小:
如果你想使用图像的原始大小,使用上面的默认构造函数,否则你可以传递你想要的位图大小
into(object : CustomTarget<Bitmap>(1980, 1080)
Java:
Glide.with(this)
.asBitmap()
.load(path)
.into(new CustomTarget<Bitmap>() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
@Override
public void onLoadCleared(@Nullable Drawable placeholder) {
}
});
旧答案:
compile 'com.github.bumptech.glide:glide:4.8.0' 及以下
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
});
compile 'com.github.bumptech.glide:glide:3.7.0' 及以下
Glide.with(this)
.load(path)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
现在您可能会看到警告 SimpleTarget is deprecated
原因:
弃用 SimpleTarget 的主要目的是警告您
它诱使您违反 Glide 的 API 合同的方式。
具体来说,它不会强制您停止使用任何
清除 SimpleTarget 后加载的资源,这可以
导致崩溃和图形损坏。
SimpleTarget 仍然可以使用,只要您确保在清除 imageView 后不使用位图。