[英]Use glide to load bitmap to ImageView
I would like to use Glide to load bitmap to ImageView after cropping and re-sizing a bitmap. 我想在裁剪和重新调整位图大小后使用Glide将位图加载到ImageView。
I don't want to use ImageView.setImageBitmap(bitmap);
我不想使用
ImageView.setImageBitmap(bitmap);
because I am loading lots of images and it might be taking up some memory, although the images are small in size, I just need to use Glide because I know it optimises image caching. 因为我正在加载大量图像并且它可能会占用一些内存,虽然图像尺寸很小,但我只需要使用Glide,因为我知道它可以优化图像缓存。
I read this post, but I didn't quite understand his solution when I tried implementing it. 我读了这篇文章,但是当我尝试实现它时,我并不完全理解他的解决方案。 So maybe someone has a cleaner and more easily understandable solution.
所以也许某人有一个更清洁,更容易理解的解决方案。
This is my code which picks up an image and creates a bitmap out of it. 这是我的代码,它拾取图像并从中创建一个位图。
I need to use glide instead of ImageView.setImageBitmap(bitmap);
我需要使用glide而不是
ImageView.setImageBitmap(bitmap);
. 。
new AsyncTask<String, Void, Void>() {
Bitmap theBitmap = null;
Bitmap bm = null;
@Override
protected Void doInBackground(String... params) {
String TAG = "Error Message: ";
try {
//Load the image into bitmap
theBitmap = Glide.
with(mContext).
load("http://example.com/imageurl").
asBitmap().
into(-1, -1).
get();
//resizes the image to a smaller dimension out of the main image.
bm = Bitmap.createBitmap(theBitmap, 0, 0, 210, 80);
} catch (final ExecutionException e) {
Log.e(TAG, e.getMessage());
} catch (final InterruptedException e) {
Log.e(TAG, e.getMessage());
} catch (final NullPointerException e) {
//
}
return null;
}
@Override
protected void onPostExecute(Void dummy) {
if (null != theBitmap) {
//Set image to imageview.
**// I would like to Use Glide to set the image view here Instead of .setImageBitmap function**
holder.mImageView.setImageBitmap(bm);
holder.mImageView.setAdjustViewBounds(true);
holder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
}
}.execute();
You don't need AsyncTask
to load image with Glide. 您不需要
AsyncTask
来使用Glide加载图像。 Glide load image asynchronys. 滑动加载图像异步。 Try to use this code:
尝试使用此代码:
Glide.with(mContext)
.load("http://example.com/imageurl")
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
// you can do something with loaded bitmap here
// .....
holder.mImageView.setImageBitmap(resource);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.