如何使用Picasso加载布局背景

15

请查看此答案,了解如何为Picasso创建自定义转换。 - NickUnuchek
6个回答

37

您可以使用 Picasso 的 Target 对象:

         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对目标对象持有弱引用,因此很可能被垃圾回收。

因此,根据其中一个问题上Jake Wharton的评论,我认为这可能是一个不错的解决方法:

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

2
这个示例的代码可能并不总是有效:你必须保持对Target的强引用,否则在图片加载之前它可能会被垃圾回收,因为Picasso对Targets使用了弱引用。 - BladeCoder
1
@BladeCoder 你说得对。我正在编辑我的回答以解决这个问题。谢谢! - mmark
我在使用Picasso的Target时遇到了一些麻烦,就像你上面展示的那样...(感激任何帮助) http://pastebin.com/uY2LnSDV - AndroidAndroidAndroid
  1. 你是在使用 Picasso 的最新版本(2.5.2)吗?
  2. 在代码片段中,你好像没有为目标对象关闭括号。
- mmark
@GokulNathKP,你能提供一下上下文吗? - mmark

9
我使用ImageView作为临时的图像容器。首先,使用Picasso将图片加载到ImageView中,然后通过使用getDrawable从该ImageView设置布局背景。
               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() {

                        }
                    });

不是最好的解决方案,但仍然是一个解决方案。 - stevyhacker
with()不再存在 - user12465043

4

以上的解决方案都不适用于我。但是@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() {
        }
    });

0
你可以试试这个,我用起来很好用。
//我在我的活动中使用了相对布局。
private RelativeLayout chatBG;

//初始化你的布局

chatBG = findViewById(R.id.chat_activity);

//使用Picasso库,最好在onStart()方法中使用以避免一些错误

Picasso.get().load("https://your/url")
            .placeholder(R.drawable.yourplaceholder_img)
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    chatBG.setBackground(new BitmapDrawable(bitmap));
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    Toast.makeText(ChatActivity.this, "Error : loading wallpaper", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });

0
作为之前答案的替代方案,可以通过在布局中创建具有match parent的ImageView来使用Picasso设置背景。
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)

0
在我的情况下,我需要使图片适应ImageView的大小,所以我不是在后台加载图片,而是将这个属性添加到ImageView中,并正常加载图片。
android:scaleType="fitXY"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接