我想在使用Glide在位圖裁剪和重新調整大小後加載位圖到ImageView。使用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();
你能告訴我你的問題是什麼?位圖爲空或您的ImageView顯示白色方塊? –
不,位圖不爲空,我需要使用Glide將位圖圖像設置爲ImageView,而不是直接設置它。 –
原因是因爲我正在加載大量需要內存緩存的圖像。你明白我的意思嗎? –