使用滑翔加载位图的ImageView [英] Use glide to load bitmap to ImageView

查看:140
本文介绍了使用滑翔加载位图的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用滑翔加载位图的ImageView裁剪后,重新调整大小的位图。

我不希望使用 ImageView.setImageBitmap(位); ,因为我加载大量的图片,并且它可能会占用一些内存,虽然图像体积小,我只需要使用滑翔因为我知道它优化了图像缓存。

我读<一个href=\"http://stackoverflow.com/questions/31867322/using-glide-to-load-bitmap-into-imageview\">this职位,但我不明白他的解决​​方案,当我试图实现它。所以,也许有人有一个更清洁,更易于理解的解决方案。

这是我的code这拾取图像,并创建一个位图出来。

我需要使用滑行,而不是 ImageView.setImageBitmap(位);

 新的AsyncTask&LT;弦乐,太虚,太虚&GT;(){
    位图theBitmap = NULL;
    位图BM = NULL;    @覆盖
    保护无效doInBackground(字符串... PARAMS){
        字符串标记=错误消息:;
        尝试{
            //加载图像为位图
            theBitmap =滑翔。
                    与(mContext)。
                    负载(http://example.com/imageurl)。
                    asBitmap()。
                    成(-1,-1)。
                    得到();            //调整图像大小,以更小的尺寸出主形象。
            BM = Bitmap.createBitmap(theBitmap,0,0,210,80);
        }赶上(最终为ExecutionException E){
            Log.e(TAG,e.getMessage());
        }赶上(最终InterruptedException的E){
            Log.e(TAG,e.getMessage());
        }赶上(最终NullPointerException异常五){
            //
        }
        返回null;
    }    @覆盖
    保护无效onPostExecute(虚空虚拟){
        如果(NULL!= theBitmap){
            //设置图像的ImageView。
            ** //我想用滑翔到这里设置相反.setImageBitmap函数的图像视图**
            holder.mImageView.setImageBitmap(BM);            holder.mImageView.setAdjustViewBounds(真);
            holder.mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        }
    }
}。执行();


解决方案

您不需要的AsyncTask 加载图像与滑翔。滑翔负荷图像asynchronys。
尝试使用这个code:

  Glide.with(mContext)
                .load(http://example.com/imageurl)
                .asBitmap()
                .into(新SimpleTarget&LT;&位图GT;(){
                    @覆盖
                    公共无效onResourceReady(位图资源,GlideAnimation&LT ;?超级位图&GT; glideAnimation){
                        //你可以做一些与加载的位图在这里                        // .....                        holder.mImageView.setImageBitmap(资源);
                    }
                });

I would like to use Glide to load bitmap to ImageView after cropping and re-sizing a bitmap.

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.

I read this post, 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);.

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

解决方案

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

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

这篇关于使用滑翔加载位图的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆