我想在裁剪和重新调整位图大小后使用Glide将位图加载到ImageView。
我不想使用ImageView.setImageBitmap(bitmap);
,因为我正在加载大量图片而且它可能占用了一些内存,虽然图像尺寸很小,我只需要使用Glide,因为我知道它优化了图像缓存。
我看了this帖子,但是当我尝试实施时,我并不完全理解他的解决方案。所以也许某人有一个更清洁,更容易理解的解决方案。
这是我的代码,它会拾取图像并从中创建一个位图。
我需要使用滑行代替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();
答案 0 :(得分:18)
您不需要AsyncTask
使用Glide加载图片。滑动加载图像异步。
尝试使用此代码:
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);
}
});