使用glide将位图加载到ImageView

Htm*_*sin 8 java android bitmap android-glide

我想在裁剪和重新调整位图大小后使用Glide将位图加载到ImageView.

我不想使用,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();
Run Code Online (Sandbox Code Playgroud)

Yur*_*kiy 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);
                    }
                });
Run Code Online (Sandbox Code Playgroud)