【问题标题】:Picasso not loading bitmap into imageview with target毕加索没有将位图加载到带有目标的图像视图中
【发布时间】:2020-06-24 19:26:24
【问题描述】:

我正在尝试将 url 加载到 ImageView 中,但 Target 似乎不像我发现的那样工作。以下是我的代码:

ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
setMethodPicture(sectionModel.getContent().get(0).getThumbnail(), methodButton);
methodsContainer.addView(methodButton);

private void setMethodPicture(final String methodPicture, final ImageView methodButton){
    methodButton.setBackgroundColor(0x000000);
    if (!StringUtils.isEmpty(methodPicture)) {
        Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                methodButton.setImageBitmap(bitmap);
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }

            @Override
            public boolean equals(Object o) {
                return methodPicture.equals(o);
            }

            @Override
            public int hashCode() {
                return methodPicture.hashCode();
            }
        };
        Picasso.with(context).load(methodPicture).into(target);
    }
}

这不会将图片加载到 imageView,但是当我这样做时,

ImageView methodButton= (ImageView) View.inflate(context, R.layout.view_home_scroll_view_image, null);
methodButton.setBackgroundColor(0x000000);
Picasso.with(context).load(sectionModel.getContent().get(0).getThumbnail()).into(methodButton);
methodsContainer.addView(methodButton);

它加载图片。 我想做第一个,这样我就可以在将其放入 ImageView 之前更改获得的位图,例如更改宽度和高度,但基于原始尺寸。

【问题讨论】:

  • 您在 logcat 中获得的任何日志?

标签: android picasso


【解决方案1】:

我发现了问题,毕加索持有对目标的弱引用。正确答案可以在这里找到:onBitmapLoaded of Target object not called on first load

【讨论】:

    【解决方案2】:

    改用 Glide,它提供了预定义的约束选项,您可以添加这些选项

    https://github.com/bumptech/glide

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-26
    • 2016-03-07
    • 2015-04-29
    • 1970-01-01
    • 2017-06-15
    • 2020-05-02
    相关资源
    最近更新 更多