Java 使用 glide 将位图加载到 ImageView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33709551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:56:31  来源:igfitidea点击:

Use glide to load bitmap to ImageView

javaandroidbitmapandroid-glide

提问by Tosin Onikute

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);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.

我不想使用,ImageView.setImageBitmap(bitmap);因为我正在加载大量图像,它可能会占用一些内存,虽然图像很小,但我只需要使用 Glide,因为我知道它优化了图像缓存。

I read thispost, 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();

回答by Yuriy Kolbasinskiy

You don't need AsyncTaskto load image with Glide. Glide load image asynchronys. Try to use this code:

您不需要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);
                    }
                });