如何使用 Picasso 加载布局背景

How To Load Layout Background Using Picasso

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

Unable to set LinearLayout BackgroundResource from URL using Picasso

您可以使用 Picasso 的目标:

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

我使用 ImageView 作为临时 ImageHolder。首先,将带有picasso的图像加载到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() {

                        }
                    });

在我的例子中,我需要图像适合 imageview 的大小,所以我没有在背景中加载图像,而是将此 属性 添加到 imageview 并正常加载图像。

android:scaleType="fitXY"

None 以上解决方案对我有用。但是@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() {
        }
    });

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

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)