【问题标题】:How To Load Layout Background Using Picasso如何使用毕加索加载布局背景
【发布时间】:2015-07-16 21:30:25
【问题描述】:

谁能给我举个例子,说明如何使用 Picasso 以编程方式更改 XML 布局的背景?我发现的所有示例都能够使用 Picasso 更新 ImageView - 但不能更新布局背景。

Unable to set LinearLayout BackgroundResource from URL using Picasso

【问题讨论】:

  • 查看this answer 了解如何为毕加索创建自定义转换

标签: android android-layout picasso


【解决方案1】:

你可以使用毕加索的目标:

         Picasso.with(this).load("http://imageUrl").into(new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
               mYourLayout.setBackground(new BitmapDrawable(bitmap));
            }

            @Override
            public void onBitmapFailed(Drawable errorDrawable) {

            }

            @Override
            public void onPrepareLoad(Drawable placeHolderDrawable) {

            }
        });

更新

正如@BladeCoder 在评论中提到的,Picasso 持有对 Target 对象的弱引用,因此它很可能被垃圾回收。

因此,在 Jake Wharton's comment 关注其中一个问题后,我认为这可能是一个好方法:

  CustomLayout mCustomLayout = (CustomLayout)findViewById(R.id.custom_layout)
  Picasso.with(this).load("http://imageUrl").into(mCustomLayout);

CustomLayout.java:

public class CustomLayout extends LinearLayout implements Target {

    public CustomLayout(Context context) {
        super(context);
    }

    public CustomLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        setBackground(new BitmapDrawable(getResources(), bitmap));
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        //Set your error drawable
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        //Set your placeholder
    }
}

【讨论】:

  • 此示例的代码可能并不总是有效:您必须保留对您的Target 的硬引用,否则它可能在加载图像之前被垃圾收集,因为 Picasso 保留对目标的弱引用。
  • @BladeCoder 你是对的。我正在编辑我的答案以解决该问题。谢谢
  • 我在使用 Picasso's Target 时遇到了一些麻烦,如您在上面所示...(感谢任何帮助)pastebin.com/uY2LnSDV
  • @NoobDev910 1) 您使用的是毕加索 (2.5.2) 的 las 版本吗? 2)在 sn-p 中,您似乎没有关闭目标对象的括号
  • @GokulNathKP 你能提供上下文吗?
【解决方案2】:

我使用 ImageView 作为临时 ImageHolder。首先,将毕加索的图像加载到 ImageView 中,并使用 getDrawable 从这个 ImageView 中设置 Layout Background。

               ImageView img = new ImageView(this);
               Picasso.with(this)
              .load(imageUri)
              .fit()
              .centerCrop()
              .into(img, new Callback() {
                        @Override
                        public void onSuccess() {

                            myLayout.setBackgroundDrawable(img.getDrawable());
                        }

                        @Override
                        public void onError() {

                        }
                    });

【讨论】:

  • 不是最好的解决方案,但仍然是一个解决方案。
  • with() 不再存在
【解决方案3】:

以上解决方案都不适合我。但是@Thiha 的解决方案是最接近的。以下对我有用:

final ImageView img = new ImageView(this);
    Picasso.with(img.getContext()).load(url).into(img, new com.squareup.picasso.Callback() {
        @Override
        public void onSuccess() {
            collapsingToolbarLayout.setBackgroundDrawable(img.getDrawable());
        }

        @Override
        public void onError() {
        }
    });

【讨论】:

    【解决方案4】:

    在我的情况下,我需要图像以适应 imageview 的大小,因此我没有在后台加载图像,而是将此属性添加到 imageview 并正常加载图像。

    android:scaleType="fitXY"
    

    【讨论】:

      【解决方案5】:

      作为先前答案的替代方案。可以通过 Piccasso 设置背景,只需在布局上创建具有匹配父级的 ImageView。

      XML:

      <ImageView
          android:id="@+id/imageView_background"
          android:layout_width="match_parent"
          android:contentDescription="@string/background"
          android:layout_height="match_parent"
          android:alpha="0.7"/>
      

      Kt:

      Picasso.get().load(R.drawable.background_green_forest).fit().centerCrop().into(view.imageView_background)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-04
        • 2015-06-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多