使用毕加索将背景图像添加到android ListView

使用毕加索将背景图像添加到android ListView,android,android-listview,background-image,picasso,Android,Android Listview,Background Image,Picasso,我需要向ListView添加背景图像。通常我会调用listview.setBackground(myImage)。但是图像来自服务器,所以我需要使用毕加索将图像加载到我的ListView的背景中。我该怎么做?选项一 定义com.squareup.picasso.Target的匿名子类 Picasso.with(yourContext) .load(yourImageUri) .into(new Target() { @Override @T

我需要向ListView添加背景图像。通常我会调用
listview.setBackground(myImage)
。但是图像来自服务器,所以我需要使用毕加索将图像加载到我的ListView的背景中。我该怎么做?

选项一 定义
com.squareup.picasso.Target的匿名子类

Picasso.with(yourContext)
      .load(yourImageUri)
      .into(new Target() {
        @Override
        @TargetApi(16)
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            int sdk = android.os.Build.VERSION.SDK_INT;
            if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
                yourListView.setBackgroundDrawable(new BitmapDrawable(bitmap));
            } else {
                yourListView.setBackground(new BitmapDrawable(getResources(), bitmap));
            }
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            // use error drawable if desired
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            // use placeholder drawable if desired
        }
    });
public class PicassoListView extends ListView implements Target {

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

    public PicassoListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    @TargetApi(16)
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        int sdk = android.os.Build.VERSION.SDK_INT;
        if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            setBackgroundDrawable(new BitmapDrawable(bitmap));
        } else {
            setBackground(new BitmapDrawable(getResources(), bitmap));
        }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        // use error drawable if desired
    }

    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {
        // use placeholder drawable if desired
    }

}
这使您可以执行以下操作:

Picasso.with(yourContext)
          .load(yourImageUri)
          .into(yourListView);

这值得检查和+1。谢谢。如果我在加载和插入之间调用resize,则永远不会调用
onBitmapLoaded
。有办法解决这个问题吗?我希望能够调整大小,这很奇怪<在两个示例中,当我调用
resize
时,都会为我调用code>onBitmapLoaded
。我调查了这个问题,发现其中似乎有关联。希望这有帮助