【问题标题】:Using Picasso to load an image into a bitmap使用毕加索将图像加载到位图中
【发布时间】:2015-09-14 00:16:55
【问题描述】:

我正在尝试使用 Picasso 库将图像从 URL 加载到位图中,但我发现的大多数示例都是指将位图加载到 ImageView 或类似的东西。

根据文档,代码应该是这样的。

public void loadImage() {

        Picasso.with(getBaseContext()).load("image url").into(new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
            }
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
                Bitmap bitImage = Bitmap(getApplicationContext(),bitmap);
            }
            @Override
            public void onBitmapFailed(Drawable arg0) {
            }
        });
    }

Bitmap bitImage = Bitmap(getApplicationContext(),bitmap); 似乎不正确,因为我收到了预期的方法调用错误。

【问题讨论】:

    标签: java android picasso


    【解决方案1】:

    看起来您没有正确创建位图,但如果我处于您的位置,我会像这样创建一个缩放位图:

    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
    
        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    

    然后将其设置为 imageView,如下所示:

    mImg.setImageBitmap(img);
    

    总体来说是这样的:

    public void loadImage() {
    
        Picasso.with(getBaseContext()).load("image url").into(new Target() {
                // ....
    
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {
                    // Pick arbitrary values for width and height
                    Bitmap resizedBitmap = getResizedBitmap(bitmap, newWidth, newHeight);
                    mImageView.setBitmap(resizedBitmap);
                }
    
                // ....
            });
        }
    }
    
    public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);
    
        // "RECREATE" THE NEW BITMAP
        Bitmap resizedBitmap = Bitmap.createBitmap(
        bm, 0, 0, width, height, matrix, false);
        bm.recycle();
        return resizedBitmap;
    }
    

    但我质疑您完全使用Target,通常这是针对非常特殊的情况。您应该在将要显示图像的同一类中调用 Picasso 的单例。通常这是在Adapter(可能是 RecyclerView 适配器)中,如下所示:

    Picasso.with(mContext)
        .load("image url")
        .into(mImageView);
    

    【讨论】:

    • 谢谢,我已经有了调整大小的方法 (developer.android.com/training/displaying-bitmaps/…)。我只需要将图像从 URL 加载到位图中(而不是直接加载到 ImageView 中),从上面的 cmets 中找出来。非常感谢。
    • 为什么要使用异步任务来加载图片。您可以在 .into(mImageView, new Callback { ... }); 中实现回调
    • 实际上,我正在尝试一种快速的方法将图像加载到位图中,然后将其传递给 Adapter (RecycleView) ,而不是从资源文件夹中加载它。我正在尝试调试此处描述的问题 stackoverflow.com/questions/32554358/… 。我可能会尝试在将要显示的类中加载图像。但我认为毕加索只能用于活动课程,或者至少这是我从一些帖子中理解的。而且我认为我需要将 Context 传递给 Adapter 类,对吧?
    • 保持Bitmaps 占用大量内存,尤其是当您必须从 URL 加载它时。让Picasso 像我上面描述的那样为您处理它,是的,将上下文传递给适配器。
    • 好的,知道了,我试试这个方法,非常感谢您的帮助。
    猜你喜欢
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多