[英]Use glide to load bitmap to ImageView
我想在裁剪和重新調整位圖大小后使用Glide將位圖加載到ImageView。
我不想使用ImageView.setImageBitmap(bitmap);
因為我正在加載大量圖像並且它可能會占用一些內存,雖然圖像尺寸很小,但我只需要使用Glide,因為我知道它可以優化圖像緩存。
我讀了這篇文章,但是當我嘗試實現它時,我並不完全理解他的解決方案。 所以也許某人有一個更清潔,更容易理解的解決方案。
這是我的代碼,它拾取圖像並從中創建一個位圖。
我需要使用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();
您不需要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);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.